我试图多次运行另一个脚本并让它们并行运行。该脚本返回1表示成功,0表示失败。我需要得到返回码的总和,以便知道有多少成功。
import multiprocessing
import subprocess
class my_class():
def value(self, state):
"""
Returns the value of the given state
"""
runs = 50
self.wins = 0
q = multiprocessing.Manager().Queue()
pool = multiprocessing.Pool(multiprocessing.cpu_count() + 2)
watcher = pool.apply_async(self.listener, (q,))
jobs = []
for i in range(runs):
job = pool.apply_async(self.worker, (state, q))
jobs.append(job)
for job in jobs:
job.get()
q.put('kill')
pool.close()
pool.join()
wins = self.wins
print "Rate: {}/{} -> {}%".format(wins, runs, float(wins)/runs*100)
sys.exit()
return float(wins)/runs
def worker(self, state, q):
p = subprocess.Popen(["python", "script.py", state], stdout=subprocess.PIPE)
out, err = p.communicate()
q.put(p.returncode)
return
def listener(self, q):
wins = 0
while True:
if not q.empty():
m = q.get()
if m == 'kill':
break
wins += m
self.wins = wins
return
代码返回:
cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed
我之前使用类似的代码写入文件,所以我不确定我做错了什么。请帮忙!
编辑:(解决方案)
感谢回应,我了解到酸洗的原因,但这不是我的问题。我的问题是如何从线程中获取返回码。通过使工作者成为不属于某个类的方法,可以很容易地实现这一点。
这是我的解决方案:
import multiprocessing
import subprocess
class my_class():
def value(self, state):
"""
Returns the value of the given state
"""
runs = 50
q = multiprocessing.Manager().Queue()
pool = multiprocessing.Pool(multiprocessing.cpu_count() + 2)
jobs = []
for i in range(runs):
job = pool.apply_async(worker, (state, q))
jobs.append(job)
wins = 0
for job in jobs:
wins += job.get()
pool.close()
pool.join()
print "Rate: {}/{} -> {}%".format(wins, runs, float(wins)/runs*100)
sys.exit()
return float(wins)/runs
def worker(state, q):
p = subprocess.Popen(["python", "script.py", state], stdout=subprocess.PIPE)
out, err = p.communicate()
return p.returncode