问题描述
试图廉价地编写基于进程的超时(同步),如下所示:
Tried to write a process-based timeout (sync) on the cheap, like this:
from concurrent.futures import ProcessPoolExecutor
def call_with_timeout(func, *args, timeout=3):
with ProcessPoolExecutor(max_workers=1) as pool:
future = pool.submit(func, *args)
result = future.result(timeout=timeout)
但是似乎将timeout
参数传递给了 future.result 并不能像宣传中那样正常工作.
But it seems the timeout
argument passed to future.result doesn't really work as advertised.
>>> t0 = time.time()
... call_with_timeout(time.sleep, 2, timeout=3)
... delta = time.time() - t0
... print('wall time:', delta)
wall time: 2.016767978668213
好.
>>> t0 = time.time()
... call_with_timeout(time.sleep, 5, timeout=3)
... delta = time.time() - t0
... print('wall time:', delta)
# TimeoutError
不正常- 5秒后解锁,而不是3秒.
Not OK - unblocked after 5 seconds, not 3 seconds.
相关问题显示了如何使用线程池或信号.如何在 n 秒后使提交给池的进程超时,而无需使用任何_private API的多处理功能?硬杀死就可以了,不需要请求彻底关闭.
Related questions show how to do this with thread pools, or with signal. How to timeout a process submitted to a pool after n seconds, without using any _private API of multiprocessing? Hard kill is fine, no need to request a clean shutdown.
推荐答案
您可能想看看 pebble
.
You might want to take a look at pebble
.
它的ProcessPool
旨在解决这个确切的问题:无需关闭整个池就可以启用超时和取消正在运行的任务.
Its ProcessPool
was designed to solve this exact issue: enable timeout and cancellation of running tasks without the need of shutting down the entire pool.
当将来超时或被取消时,工作人员实际上被终止,从而有效地停止了计划功能的执行.
When a future times out or is cancelled, the worker gets actually terminated effectively stopping the execution of the scheduled function.
超时:
pool = pebble.ProcessPool(max_workers=1)
future = pool.schedule(func, args=args, timeout=1)
try:
future.result()
except TimeoutError:
print("Timeout")
示例:
def call_with_timeout(func, *args, timeout=3):
pool = pebble.ProcessPool(max_workers=1)
with pool:
future = pool.schedule(func, args=args, timeout=timeout)
return future.result()
这篇关于任何有效的并发.超时时间有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!