超时加入一组python进程 [英] Join a group of python processes with a timeout
问题描述
我正在使用python的多处理库来创建多个进程.
I'm using python's multiprocessing library to create several processes.
from multiprocessing import Process
processes = [Process(target=function) for function in FUNCTIONS]
for p in processes:
p.start()
我希望它们运行一段时间,然后如果尚未完成,请终止它们.
I want them to run for some duration and then if they have not completed, terminate them.
DURATION = 3600
执行此操作的不良方法如下(这很糟糕,因为如果进程比DURATION更快完成,它仍要等待DURATION的所有时间):
A bad way to do it is as follows (bad because if the processes finish faster than DURATION, it still waits for all of DURATION):
from time import sleep
sleep(duration)
for p in processes:
p.join(0)
p.terminate()
另一种不好的方法(很糟糕,因为它可能需要N * DURATION才能完成,其中N是进程数):
Another bad way to do it (bad because it can possibly take N * DURATION to finish, where N is the number of processes):
for p in processes:
p.join(DURATION)
p.terminate()
执行此操作的好方法是什么?
What is a good way to do this?
推荐答案
我相信这可以满足您的要求,而无需任何轮询,它只会等待您指定的DURATION.
I believe this does what you want without any polling required, and it will only wait up to your specified DURATION.
time_waited = 0
then = time.time()
for p in processes:
if time_waited >= DURATION:
p.join(0)
p.terminate()
p.join(DURATION - time_waited)
time_waited = time.time() - then
这篇关于超时加入一组python进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!