pyinstaller生成的.exe在键盘中断后为什么还会重启?
我不小心造出了一个怪物。
在解释器里一切运行得很好,但我用 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()
呢?有时候它甚至在我按下键盘中断之前就会这样做。
1 个回答
2
我猜测,ImageGrab
模块是通过multiprocessing
模块来启动一个子进程,这个子进程负责收集图像。但是,这个子进程在使用单文件的PyInstaller可执行文件时会出现问题。问题在于,Windows没有一个叫做spawn的命令,它只是重新运行这个可执行文件。为了避免你的程序随机重新运行,你需要调整一些环境变量。