在我的项目我使用Python的multiprocessing
库创建于__main__多个进程。该项目使用PyInstaller 2.1.1打包到单个Windows EXE中。PyInstaller一个Windows EXE失败,多处理
创建新的进程,像这样:
from multiprocessing import Process
from Queue import Empty
def _start():
while True:
try:
command = queue.get_nowait()
# ... and some more code to actually interpret commands
except Empty:
time.sleep(0.015)
def start():
process = Process(target=_start, args=args)
process.start()
return process
而且在__main__:
if __name__ == '__main__':
freeze_support()
start()
不幸的是,打包应用程序时,成EXE和启动它,我得到WindowsError
5或6(似乎随机)在这条线上:
command = queue.get_nowait()
PyInstaller的主页上的配方声称我要修改我的代码,以便将应用程序打包为单个文件时,多处理在Windows中。
我在这里再现代码:
import multiprocessing.forking
import os
import sys
class _Popen(multiprocessing.forking.Popen):
def __init__(self, *args, **kw):
if hasattr(sys, 'frozen'):
# We have to set original _MEIPASS2 value from sys._MEIPASS
# to get --onefile mode working.
# Last character is stripped in C-loader. We have to add
# '/' or '\\' at the end.
os.putenv('_MEIPASS2', sys._MEIPASS + os.sep)
try:
super(_Popen, self).__init__(*args, **kw)
finally:
if hasattr(sys, 'frozen'):
# On some platforms (e.g. AIX) 'os.unsetenv()' is not
# available. In those cases we cannot delete the variable
# but only set it to the empty string. The bootloader
# can handle this case.
if hasattr(os, 'unsetenv'):
os.unsetenv('_MEIPASS2')
else:
os.putenv('_MEIPASS2', '')
class Process(multiprocessing.Process):
_Popen = _Popen
class SendeventProcess(Process):
def __init__(self, resultQueue):
self.resultQueue = resultQueue
multiprocessing.Process.__init__(self)
self.start()
def run(self):
print 'SendeventProcess'
self.resultQueue.put((1, 2))
print 'SendeventProcess'
if __name__ == '__main__':
# On Windows calling this function is necessary.
if sys.platform.startswith('win'):
multiprocessing.freeze_support()
print 'main'
resultQueue = multiprocessing.Queue()
SendeventProcess(resultQueue)
print 'main'
我这个“解决方案”无奈的是,一,这是绝对不清楚它到底是什么补丁,还有,两个,这是写在这样的它变得不可能推断出哪些部分是该溶液中,并且其旋绕方式只是例示。
任何人都可以分享在这个问题上的一些灯,并提供准确的洞察力需要在一个项目中,使多在PyInstaller内置单文件的Windows可执行文件改变什么呢?
是否配方解决这个问题? – dano
那么,目前还不清楚(至少对我来说)如何应用配方。只是将上面的代码粘贴到我的主Python脚本中也不起作用,因为它会引发两个与我的Python脚本无关的异常。这告诉我这个配方是根本上有缺陷的。 – nikola
如果您只是将配方作为独立脚本运行,它是否运行没有错误? – dano