繁体   English   中英

Python 3.4多处理不适用于py2exe

[英]Python 3.4 multiprocessing does not work with py2exe

这与此问题几乎相同,但那里给出的解决方案(调用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()

它作为纯python运行时工作得很好。 但是,当打包为可执行文件时,这是我得到的错误:

Unknown option: --
usage: <path to start.exe> [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.

这显然是通过电话引起的

python.exe --multiprocessing-fork

如果我不调用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

我在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()

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

尝试在文档中建议的修复:

multiprocessing.set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM