Python 3.4多重处理不适用于py2exe [英] Python 3.4 multiprocessing does not work with py2exe

查看:107
本文介绍了Python 3.4多重处理不适用于py2exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与几乎相同,但是给定的解决方案(调用freeze_support())对我不起作用。

This is pretty much the same as this question, but the given solution there (calling freeze_support()) does not work for me.

我有以下名为start.py的脚本,使用py2exe(版本0.9.2.2)构建一个独立的可执行文件。我在同一目录中也有python.exe。

I have the following script called start.py that I use to build a standalone executable with py2exe (version 0.9.2.2). I also have python.exe in the same directory.

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运行时,它的工作效果非常好。但是,当打包为可执行文件时,这是我得到的错误:

It works perfectly fine when run as pure python. However, when packaged as an executable, this is the error I get:

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

这显然是由于打电话引起的

This is clearly caused by calling

python.exe --multiprocessing-fork

如果我不这样做t调用set_executable()和freeze_support(),子进程只是启动exe并以__main__身份运行,从而导致无休止的新进程链打印 Parent,而从未打印 Child。

If I don't call set_executable() and freeze_support(), the child process just starts up the exe and runs as __main__, causing an endless chain of new processes to print "Parent" while "Child" is never printed.

调用freeze_support()似乎唯一要做的就是如果我不调用multiprocessing.set_executable()会导致子进程引发以下错误

The only thing calling freeze_support() seems to do is cause the child process to raise the following error if I don't call 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尝试了以上所有方法,并且结果相同。任何帮助将不胜感激。

I'm using Python 3.4 32-bit running on Windows 8.1 64 bit. I've tried everything above using cx-Freeze with the same results. Any help would be much appreciated.

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

Even when using this example straight out of the docs:

from multiprocessing import Process, freeze_support

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

if __name__ == '__main__':
    freeze_support()
    Process(target=f).start()

当子进程调用Frozen_support()。

I get the same NameError when the child process calls freeze_support().

推荐答案

尝试建议的修复方法在文档中

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

还请注意,您需要在生成任何新进程之前调用

Also note that you need to call this before spawning any new processes.

这篇关于Python 3.4多重处理不适用于py2exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆