Python 多处理线程池关闭超时

Python 多处理线程池关闭超时,python,multiprocessing,Python,Multiprocessing,我想用pool对象的close方法优雅地停止pool worker,但终止那些在10秒内没有完成执行的工作 started_at = int(time.time()) p.close() # this is blocking if (int(time.time()) - started_at >= 10): p.terminate() 像这样的。有什么想法吗 我还考虑过向线程发送SIGTERMs,但它们共享相同的pid,所以我不能这样做。似乎我第一次没有得到这个问题 您可能可以使用a

我想用pool对象的close方法优雅地停止pool worker,但终止那些在10秒内没有完成执行的工作

started_at = int(time.time())
p.close() # this is blocking
if (int(time.time()) - started_at >= 10):
  p.terminate()
像这样的。有什么想法吗


我还考虑过向线程发送SIGTERMs,但它们共享相同的pid,所以我不能这样做。

似乎我第一次没有得到这个问题

您可能可以使用apply\u async将p.close调用发送到另一个进程,并查看apply\u async调用是否没有及时完成,只需调用p.terminate即可


有关应用异步的详细信息,请参考。

如果使用线程池,可以使用全局变量,例如stopthreads

工作线程中运行的函数应经常检查此变量,并在设置为True时退出:


对不起,我的情况是ThreadPool。修好它
def worker(data):
    while True:
        if stopthreads:
            return None
        # do other things