컨텍스트다량의 원인이 교착 상태
나는 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()
그리고 여기 가능한 출력 : process.is_alive()
몇 반복 후 항상 곁에 사실,
➜ ~ python m.py
..0
1
........8
.9
.......10
......11
........12
13
........14
........16
........................................................................................
당신이 볼 수 있듯이, 프로세스는 것입니다 절대 가입하지 마라.
만약 내가 CTRL-C 스크립트이 스택 트레이스 수 :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 맥 OS에 파이썬 3.5.2를 사용합니다.
모든 종류의 도움을 주셔서 감사합니다.
편집
내가 파이썬 2.7를 사용하여 시도, 그리고 그것을 잘 작동합니다. 파이썬 3.5 문제 일 수 있습니까?