我正在尝试使用多处理模块设置并行作业代码。我想在10秒内终止所有的孩子工作。我在进程连接函数中使用timeout参数创建了以下代码。但是,我发现所有作业的终止时间都取决于超时值和作业数。如何计算准确时间?
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()
答案 0 :(得分:1)
您可以先计算十秒超时,然后在一个循环中等待并终止作业。您只等待超时未到达。
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()