我试图使用多处理模块设置并行作业的代码。我想在 10 秒内终止所有子作业。我在进程连接函数中使用超时参数制作了以下代码。但是,我发现所有作业的终止时间取决于超时值和作业数。如何计算准确时间?
import time
from multiprocessing import Manager, Process
def f(x, dic):
time.sleep(60)
print "Time: ", x
dic[x] = "Done"
mng = Manager()
dic = mng.dict()
jobs = [Process(target=f, args=(i, dic)) for i in range(50)]
for job in jobs:
job.start()
for job in jobs:
job.join(timeout=10)
for job in jobs:
job.terminate()
for job in jobs:
job.join()
print dic.keys()
您可以先计算十秒超时,然后在单个循环中等待并终止作业。只有在超时尚未到来时,您才等待。
end_at = time.time() + 10
while jobs:
job = jobs.pop()
delta = end_at - time.time()
if delta > 0:
job.join(timeout=delta)
job.terminate()
job.join()