赞
踩
这是一种无需更改工作函数即可完成此操作的方法.我们的想法是将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.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。