我希望使用池对象的close()
方法优雅地阻止池工作者,但terminate()
那些在10秒内没有完成执行的人。
started_at = int(time.time())
p.close() # this is blocking
if (int(time.time()) - started_at >= 10):
p.terminate()
像这样的东西。任何的想法? :)
我还想过将SIGTERM
发送给线程,但是它们共享相同的pid,所以我不能这样做。
答案 0 :(得分:1)
如果您正在使用线程池,则可以使用全局变量(例如stopthreads
)。
工作线程中运行的函数应经常检查此变量,并在设置为True
时退出:
def worker(data):
while True:
if stopthreads:
return None
# do other things
答案 1 :(得分:0)
好像我第一次没有得到这个问题。
您可以使用serv.ice:
将p.close()
来电发送给其他流程,并查看apply_async
来电是否未及时完成,只需致电{{ 1}}。
有关apply_async
的更多信息,请参阅docs。