【发布时间】:2018-07-10 11:23:35
【问题描述】:
目前尚不清楚如何在 python 中正确超时 joblib 的 Parallel 的工作人员。其他人也有类似的问题here、here、here 和here。
在我的示例中,我使用 50 个 joblib 工人池和 threading 后端。
并行调用(线程):
output = Parallel(n_jobs=50, backend = 'threading')
(delayed(get_output)(INPUT)
for INPUT in list)
这里,Parallel 在len(list) <= n_jobs 时立即挂起而没有错误,但仅在n_jobs => -1 时才挂起。
为了规避这个问题,人们向instructions 提供了如何使用multiprocessing 为Parallel 函数(上例中的get_output(INPUT))创建超时装饰器:
主要功能(装饰):
@with_timeout(10) # multiprocessing
def get_output(INPUT): # threading
output = do_stuff(INPUT)
return output
多处理装饰器:
def with_timeout(timeout):
def decorator(decorated):
@functools.wraps(decorated)
def inner(*args, **kwargs):
pool = multiprocessing.pool.ThreadPool(1)
async_result = pool.apply_async(decorated, args, kwargs)
try:
return async_result.get(timeout)
except multiprocessing.TimeoutError:
return
return inner
return decorator
将装饰器添加到原本可以工作的代码中会导致在约 2 倍超时长度加上 eclipse 崩溃后发生内存泄漏。
装饰器的泄漏点在哪里?
如何在 python 中的多处理期间使线程超时?
【问题讨论】:
-
我是原来的OP。我的内部功能使用硒。对于 selenium 上下文,我找到了一种直接使内部函数超时的方法。根据您的情况,这可能/可能不适用 - 请告诉我,我会直接回答
-
在我的帖子下回答。
标签: web-scraping screen-scraping python-multiprocessing python-multithreading joblib