Python 3.4 multiprocessing does not work with py2exe

╄→гoц情女王★ 提交于 2019-12-22 08:11:24

问题


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

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

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

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.

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

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.

EDIT: 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()

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


回答1:


Try the suggested fix in the docs:

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

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



来源:https://stackoverflow.com/questions/29115428/python-3-4-multiprocessing-does-not-work-with-py2exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!