当前位置:   article > 正文

python 超时退出程序_python – 如何在超时后中止multiprocessing.Pool中的任务?

python multiprocessing.pool timeout

这是一种无需更改工作函数即可完成此操作的方法.我们的想法是将worker包装在另一个函数中,该函数将在后台线程中调用worker,然后等待超时秒的结果.如果超时到期,则会引发异常,这将突然终止正在执行的线程工作者:

import multiprocessing

from multiprocessing.dummy import Pool as ThreadPool

from functools import partial

def worker(x, y, z):

pass # Do whatever here

def collectMyResult(result):

print("Got result {}".format(result))

def abortable_worker(func, *args, **kwargs):

timeout = kwargs.get('timeout', None)

p = ThreadPool(1)

res = p.apply_async(func, args=args)

try:

out = res.get(timeout) # Wait timeout seconds for func to complete.

return out

except multiprocessing.TimeoutError:

print("Aborting due to timeout")

p.terminate()

raise

if __name__ == "__main__":

pool = multiprocessing.Pool()

featureClass = [[1000,k,1] for k in drange(start,end,step)] #list of arguments

for f in featureClass:

abortable_func = partial(abortable_worker, worker, timeout=3)

pool.apply_async(abortable_func, args=f,callback=collectMyResult)

pool.close()

pool.join()

任何超时的函数都会引发multiprocessing.TimeoutError.请注意,这意味着在发生超时时不会执行回调.如果这是不可接受的,只需更改abortable_worker的except块以返回一些内容而不是调用raise.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/74921
推荐阅读
相关标签
  

闽ICP备14008679号