概要
python2でmultiprocessing中にエラーが発生すると応答が帰ってこなくなる。
この原因と解決法が知りたいです。
環境
OS: CentOS7
言語:python2.7
やりたかった事
特定のコマンドを並列で実行し、結果を取得。
ソース
python
1#!/bin/env python 2 3import subprocess 4from multiprocessing import Pool 5 6def testSP (cmd): 7 print(cmd) 8 try: 9 ret=subprocess.check_output(cmd) 10 return {"rc":0,"stdout":ret} 11 except subprocess.CalledProcessError as e: 12 print("catch CalledProcessError") 13 print(e) 14 return {"rc":1,"stdout":e} 15 except: 16 return {"rc":255,"stdout":"-"} 17######################## 18 19print("==true==================") 20print(testSP("true")) 21 22print("==false==================") 23print(testSP("false")) 24 25print("==subprocess==================") 26p = Pool(2) 27ret=p.map(testSP,["true","false"]) 28print(ret)
実行結果
# ./test.py ==true================== true {'stdout': '', 'rc': 0} ==false================== false catch CalledProcessError Command 'false' returned non-zero exit status 1 {'stdout': CalledProcessError(), 'rc': 1} ==subprocess================== true false catch CalledProcessError Command 'false' returned non-zero exit status 1 Exception in thread Thread-3: Traceback (most recent call last): File "/usr/lib64/python2.7/threading.py", line 811, in __bootstrap_inner self.run() File "/usr/lib64/python2.7/threading.py", line 764, in run self.__target(*self.__args, **self.__kwargs) File "/usr/lib64/python2.7/multiprocessing/pool.py", line 376, in _handle_results task = get() TypeError: ('__init__() takes at least 3 arguments (1 given)', <class 'subprocess.CalledProcessError'>, ()) 以降応答なし、ctrl+cを押しても下記エラーが出てプロンプトは戻らない ●"ctrl+c"を押下● Process PoolWorker-2: Traceback (most recent call last): File "/usr/lib64/python2.7/multiprocessing/process.py", line 258, in _bootstrap self.run() File "/usr/lib64/python2.7/multiprocessing/process.py", line 114, in run self._target(*self._args, **self._kwargs) File "/usr/lib64/python2.7/multiprocessing/pool.py", line 102, in worker task = get() File "/usr/lib64/python2.7/multiprocessing/queues.py", line 374, in get racquire() KeyboardInterrupt
以上、よろしくおねがいします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/12 01:56