コンテキスト多量原因デッドロック
私はmultiprocessing.ThreadPool内部multiprocessing.Processを実行する必要があります。 最初は不思議そうですが、これは、私がC++共有ライブラリを使用しているために発生する可能性のあるsegfaultを扱う唯一の方法です。 segfaultが追加された場合、そのプロセスは強制終了され、process.exitcodeをチェックしてそれを処理できます。
問題
しばらくして、私はプロセスに参加しようとしていたときに、デッドロックが追加。
import sys, time, multiprocessing
from multiprocessing.pool import ThreadPool
def main():
# Launch 8 workers
pool = ThreadPool(8)
it = pool.imap(run, range(500))
while True:
try:
it.next()
except StopIteration:
break
def run(value):
# Each worker launch it own Process
process = multiprocessing.Process(target=run_and_might_segfault, args=(value,))
process.start()
while process.is_alive():
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(0.1)
# Will never join after a while, because of a mystery deadlock
process.join()
# Deals with process.exitcode to log errors
def run_and_might_segfault(value):
# Load a shared library and do stuff (could throw c++ exception, segfault ...)
print(value)
if __name__ == '__main__':
main()
そして、ここでは、可能な出力である:ここで
は簡易版私のコードであるあなたが見ることができるように
➜ ~ python m.py
..0
1
........8
.9
.......10
......11
........12
13
........14
........16
........................................................................................
、process.is_alive()
は数回の反復後なかれ真である、プロセスは意志参加しないでください。
I CTRL-Cスクリプトこのスタックトレースを取得する場合:MacOSの上のpython 3.5.2を使用して
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/multiprocessing/pool.py", line 680, in next
item = self._items.popleft()
IndexError: pop from an empty deque
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "m.py", line 30, in <module>
main()
File "m.py", line 9, in main
it.next()
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5 /lib/python3.5/multiprocessing/pool.py", line 684, in next
self._cond.wait(timeout)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5 /lib/python3.5/threading.py", line 293, in wait
waiter.acquire()
KeyboardInterrupt
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5 /lib/python3.5/multiprocessing/popen_fork.py", line 29, in poll
pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt
PS 。
ご協力いただきありがとうございます。
編集
私は、Python 2.7を使用してみました、それがうまく機能しています。 Python 3.5の問題のみである可能性がありますか?