私のプロジェクトでは、Pythonのmultiprocessing
ライブラリを使用して__main__に複数のプロセスを作成しています。プロジェクトは、PyInstaller2.1.1を使用して単一のWindowsEXEにパッケージ化されています。
私は次のような新しいプロセスを作成します。
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'
この「解決策」に対する私の不満は、1つは、パッチが正確に何であるかが完全に不明確であり、2つは、どの部分が解決策であり、どの部分が単なる解決策であるかを推測することが不可能になるほど複雑な方法で書かれていることです。図。
誰かがこの問題についていくつかの光を共有し、PyInstallerで構築された単一ファイルのWindows実行可能ファイルでマルチプロセッシングを可能にするプロジェクトで正確に何を変更する必要があるかについての洞察を提供できますか?
このPyInstallerチケットを見つけた後、私自身の質問に答えます。
どうやら私たちがしなければならないのは、以下に示すようにProcess
(と_Popen
)クラスを提供し、の代わりにそれを使用することだけですmultiprocessing.Process
。Windowsでのみ機能するようにクラスを修正および簡略化しました。* ixシステムでは、異なるコードが必要になる場合があります。
完全を期すために、上記の質問からの適合サンプルを次に示します。
import multiprocessing
from Queue import Empty
class _Popen(multiprocessing.forking.Popen):
def __init__(self, *args, **kw):
if hasattr(sys, 'frozen'):
os.putenv('_MEIPASS2', sys._MEIPASS)
try:
super(_Popen, self).__init__(*args, **kw)
finally:
if hasattr(sys, 'frozen'):
os.unsetenv('_MEIPASS2')
class Process(multiprocessing.Process):
_Popen = _Popen
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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加