Python子进程在调用进程完成之前不会运行

2024-10-01 02:40:14 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑1-添加了更多代码 我不确定过程通信是需要的,这是我从其他stackoverflow代码中找到的建议之一(抱歉,昨晚我太累了,在问这个问题之前没有想太多。)

我应该补充一点,我不是一个有经验的程序员(机械工程师),你可以从我的代码中看出

在我的Gui中,我有一个按钮来调用一个子进程

子流程(屏幕截图-命令提示符)但如果一个png文件在屏幕上被裁剪,它就不会产生错误。在

这使我认为,直到事件完成,子流程才真正运行

我想在一个按钮按下后调用该进程几次,并在每次生成后移动它生成的文件

如果我使用程序等待(),进程无限期挂起。在

我该怎么阻止这一切?在

# function to take a single image called 'fileName' and place it in directory 'dir'
def takeImage(dir,fileName):

    # calculate the view to capture to get the whole display window in.
    clientRect = win32gui.GetClientRect(win32gui.GetForegroundWindow())
    windowRect = win32gui.GetWindowRect(win32gui.GetForegroundWindow())
    print(windowRect)
    windowSize = [windowRect[2]-windowRect[0],windowRect[3]-windowRect[1]]
    print(windowSize)
    print(clientRect)

    diffSize = [windowSize[0] -clientRect[2], windowSize[1] - clientRect[3]]
    lrbBorder = diffSize[0]/2
    topBorder = diffSize[1] - lrbBorder

    print("sizeDiff = " + str(diffSize))
    windowName = win32gui.GetWindowText(win32gui.GetForegroundWindow())
    handleId = win32gui.GetForegroundWindow()


    leftMar = designLabel.GetPosition()[0] + lrbBorder
    topMar = designLabel.GetPosition()[1]  + topBorder + designLabel.GetSize()[1]
    rightMar = leftMar + scene.width
    bottMar = topMar+scene.height

    margins = [leftMar,topMar,rightMar,bottMar]

    print(margins)

    # now print the view.
    #command_line = r"screenshot-cmd -wt '" + windowName + "' -rc " + str(margins[0]) + " " + str(margins[1]) + " " + str(margins[2]) + " " + str(margins[3]) + " -o " + fileName

    command_line = r"screenshot-cmd -wt '" + windowName + "' -rc " + str(margins[0]) + " " + str(margins[1]) + " " + str(margins[2]) + " " + str(margins[3]) + " -o " + fileName

    print(command_line)
    args = shlex.split(command_line)
    proc = subprocess.Popen(args)
    proc.wait() 

    wx.Yield()


    if not os.path.isdir(dir):
        os.makedirs(dir)

    newPath = os.path.join(dir,fileName)

    if os.path.exists(newPath):
        os.remove(newPath)

    oldPath = os.path.join(os.getcwd(), fileName)
    print("Old Path: " + oldPath)
    print("Exists: " + str(os.path.exists(oldPath)))
    shutil.move(oldPath,newPath)

    return

#event called upon clicking 'takeTenImag' button
def takeTenImgE(evt):
    global designNo
    global workingDirectory
    global numDesigns
    fileNameRoot = "test_"
    fileExtention = ".png"

    # check there are at least 10 designs
    if numDesigns > 9 and os.path.exists(workingDirectory):
        # find directory path to put images in 
        dir = os.path.join(workingDirectory, "images")
        # for each design

        for x in range(10):
            print("design =" + str(designNo))
            fileName = fileNameRoot + str(designNo) + fileExtention
            print("------------------")
            print("for x = " + str(x) + " "  + fileName)
            #   create image and save
            print(dir)
            takeImage(dir,fileName)
            #move to next design

            wx.PostEvent(forwardDesign, wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, forwardDesign.GetId()) ) 
            wx.Yield()
            print("design =" + str(designNo))
return

takeTenImg = wx.Button(p, label='Take Ten Images', pos=(rb + visScaleText.GetSize()[0]+10,takeImg.GetPosition()[1]+5 +takeImg.GetSize()[1]), size = (100,30))
takeTenImg.Bind(wx.EVT_BUTTON, takeTenImgE)

https://code.google.com/p/screenshot-cmd/


Tags: topathinosdirfilenamewxprint
2条回答

我发现错误在我调用子进程中。在

我用的是:

command_line = r"screenshot-cmd -wt '" + windowName + ...." 
args = shlex.split(command_line)
subprocess.call(args,shell=True)

将此更改为:

^{pr2}$

解决了这个问题。在

奇怪的是,这两个选项在不在wx按钮单击事件(即从命令行启动的python脚本)中工作时都有效,但只有第二个选项在wx按钮单击事件中工作。 如果有人能告诉我为什么我会非常感激。在

编辑: 经过进一步调查,挂起是由于试图在截图cmd中指定活动窗口造成的。在

为了解决这个问题,我用 windowRect=win32gui.GetWindowRect(win32图形用户界面())

然后在不指定窗口的情况下使用screenshot cmd。在

这解决了所有的问题,尽管还不清楚为什么这会导致问题

巴纳比,你可能把你的子流程的使用复杂化了。Popen通常用于在进程运行期间需要与进程通信时使用。从它的声音来看,您不需要这样做,所以可能需要使用一个更高级别的函数。请参阅the docs关于子进程的各种调用,也许可以尝试使用调用方法。您需要shell=True,如this one等SO问题中所述。在

相关问题 更多 >