【发布时间】:2018-10-12 17:21:39
【问题描述】:
我有一个 100-1000 个时间序列路径和一个相当昂贵的模拟,我想并行化它们。但是,我使用的库在极少数情况下会挂起,我想让它对这些问题具有鲁棒性。这是当前设置:
with Pool() as pool:
res = pool.map_async(simulation_that_occasionally_hangs, (p for p in paths))
all_costs = res.get()
我知道 get() 有一个 timeout 参数,但如果我理解正确,它适用于 1000 条路径的整个过程。我想做的是检查是否有任何 single 模拟耗时超过 5 分钟(正常路径需要 4 秒),如果是,则停止该路径并继续 get() 其余部分。
编辑:
pebble 中的测试超时
def fibonacci(n):
if n == 0: return 0
elif n == 1: return 1
else: return fibonacci(n - 1) + fibonacci(n - 2)
def main():
with ProcessPool() as pool:
future = pool.map(fibonacci, range(40), timeout=10)
iterator = future.result()
all = []
while True:
try:
all.append(next(iterator))
except StopIteration:
break
except TimeoutError as e:
print(f'function took longer than {e.args[1]} seconds')
print(all)
错误:
RuntimeError: I/O operations still in flight while destroying Overlapped object, the process may crash
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\anaconda3\lib\multiprocessing\spawn.py", line 99, in spawn_main
new_handle = reduction.steal_handle(parent_pid, pipe_handle)
File "C:\anaconda3\lib\multiprocessing\reduction.py", line 87, in steal_handle
_winapi.DUPLICATE_SAME_ACCESS | _winapi.DUPLICATE_CLOSE_SOURCE)
PermissionError: [WinError 5] Access is denied
【问题讨论】:
标签: python multiprocessing pool robustness