1

我无意中创造了科学怪人。

一切都在解释器中运行良好,但在运行python pyinstaller.py --onefile myFile.py生成 .exe 后,运行时它开始出现奇怪的行为。它似乎随机调用它的main()函数——即使在键盘中断爆发后,它也会自我复活。

代码如下所示:

def main():
    print 'DO NOT CLOSE!'
    count = 0
    lastTime =  ((time.localtime()[4])*60) + (time.localtime()[5])
    sShot = ImageGrab.grab()
    saveSnap(count, lastTime)
    currentScreen = grab()
    while True:
        if currentScreen == grab():
            pass
        else:

            count += 1
            saveSnap(count, lastTime)
            currentScreen = grab()
            lastTime = ((time.localtime()[4])*60) + (time.localtime()[5])


if __name__ == '__main__':
    main()

非常简单的东西,就像我说的,它在解释器中运行良好。它所做的只是在屏幕上搜索更改,然后为快照添加时间戳。

什么会导致随机开火main()?它有时甚至在键盘中断之前就会这样做。

4

1 回答 1

2

我的猜测是该ImageGrab模块使用该multiprocessing模块来启动一个收集图像的子进程,并且该子进程会导致单文件 PyInstaller 可执行文件出现问题。问题是 Windows 没有 spawn 命令,它只是重新运行可执行文件。您必须调整一些环境变量以避免程序随机重新运行。

于 2015-08-18T16:50:54.670 回答