简短问题:为什么在 Windows 操作系统上调用 multiprocessing 模块的函数时,pyinstaller 生成的 python 可执行文件会打开新的窗口实例
我有一个使用 pyside 编写的 GUI 代码。当我们单击简单按钮时,它将在另一个进程中计算阶乘(使用多处理模块)。当我运行 python 程序时,它按预期工作。但是在我使用 PyInstaller 创建可执行文件后,当我使用 exe 运行时,它会在调用多处理模块的函数时创建新窗口。这是重现问题的代码和分步过程。
代码:
import sys
import multiprocessing
from PySide import QtGui
from PySide import QtCore
def factorial():
f = 4
r = 1
for i in reversed(range(1, f+1)):
r *= i
print 'factorial', r
class MainGui(QtGui.QWidget):
def __init__(self):
super(MainGui, self).__init__()
self.initGui()
def initGui(self):
b = QtGui.QPushButton('click', self)
b.move(30, 30)
b.clicked.connect(self.onClick)
self.resize(600, 400)
self.show()
def onClick(self):
print 'button clicked'
self.forkProcess()
def forkProcess(self):
p = …Run Code Online (Sandbox Code Playgroud) 这与此问题几乎相同,但那里给出的解决方案(调用freeze_support())对我不起作用.
我有以下脚本叫做start.py,我用它来构建一个带py2exe的独立可执行文件(版本0.9.2.2).我也在同一目录中有python.exe.
import multiprocessing
def main():
print('Parent')
p = multiprocessing.Process(target=new_process)
multiprocessing.set_executable('python.exe')
p.start()
p.join()
def new_process():
print('Child')
if __name__ == '__main__':
multiprocessing.freeze_support()
main()
Run Code Online (Sandbox Code Playgroud)
它作为纯python运行时工作得很好.但是,当打包为可执行文件时,这是我得到的错误:
Unknown option: --
usage: <path to start.exe> [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
Run Code Online (Sandbox Code Playgroud)
这显然是通过电话引起的
python.exe --multiprocessing-fork
Run Code Online (Sandbox Code Playgroud)
如果我不调用set_executable()和freeze_support(),则子进程只启动exe并以__main__运行,导致无限链的新进程打印"Parent"而"Child"从不打印.
调用freeze_support()似乎唯一能做的就是如果我不调用multiprocessing.set_executable()会导致子进程引发以下错误
Traceback (most recent call last):
File "start.py", line 17, in <module>
multiprocessing.freeze_support()
File "C:\Python34\Lib\multiprocessing\context.py", line 148, in freeze_support
freeze_support()
File "C:\Python34\Lib\multiprocessing\spawn.py", line 67, in …Run Code Online (Sandbox Code Playgroud)