Python 3.4多处理不适用于py2exe

Mob*_*ous 5 python py2exe cx-freeze python-3.4 python-multiprocessing

这与此问题几乎相同,但那里给出的解决方案(调用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 freeze_support
    main()
NameError: name 'main' is not defined
Run Code Online (Sandbox Code Playgroud)

我在Windows 8.1 64位上使用Python 3.4 32位运行.我已经尝试了以上所有使用cx-Freeze的相同结果.任何帮助将非常感激.

编辑:即使直接从文档中使用此示例:

from multiprocessing import Process, freeze_support

def f():
    print('hello world!')

if __name__ == '__main__':
    freeze_support()
    Process(target=f).start()
Run Code Online (Sandbox Code Playgroud)

当子进程调用freeze_support()时,我得到相同的NameError.

Pat*_*ins 2

尝试文档中建议的修复:

multiprocessing.set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))
Run Code Online (Sandbox Code Playgroud)

另请注意,您需要在生成任何新进程之前调用此函数。