简体   繁体   English

多处理中的工作者是否有办法.Pool的apply_async可以捕获错误并继续?

[英]Is there a way for workers in multiprocessing.Pool's apply_async to catch errors and continue?

When using multiprocessing.Pool's apply_async() , what happens to breaks in code? 当使用multiprocessing.Pool的apply_async() ,代码中断会发生什么? This includes, I think, just exceptions, but there may be other things that make the worker functions fail. 我认为这包括异常,但可能还有其他因素导致工作者功能失败。

import multiprocessing as mp
pool = mp.Pool(mp.cpu_count())
for f in files:
    pool.apply_async(workerfunct, args=(*args), callback=callbackfunct) 

As I understand it right now, the process/worker fails (all other processes continue) and anything past a thrown error is not executed, EVEN if I catch the error with try/except. 正如我现在所理解的那样,进程/工作程序失败(所有其他进程都继续)并且没有执行抛出错误的任何事情,即使我用try / except捕获错误也是如此。

As an example, usually I'd except Errors and put in a default value and/or print out an error message, and the code continues. 作为一个例子,通常我除了错误并输入默认值和/或打印出错误消息,然后代码继续。 If my callback function involves writing to file, that's done with default values. 如果我的回调函数涉及写入文件,则使用默认值完成。

This answerer wrote a little about it : 这位回答者写了一些关于它的文章

I suspect the reason you're not seeing anything happen with your example code is because all of your worker function calls are failing. 我怀疑你没有看到你的示例代码发生任何事情的原因是因为所有的工作者函数调用都失败了。 If a worker function fails, callback will never be executed. 如果worker函数失败,则永远不会执行回调。 The failure won't be reported at all unless you try to fetch the result from the AsyncResult object returned by the call to apply_async. 除非您尝试从apply_async调用返回的AsyncResult对象中获取结果,否则根本不会报告失败。 However, since you're not saving any of those objects, you'll never know the failures occurred. 但是,由于您没有保存任何这些对象,因此您永远不会知道发生的故障。 If I were you, I'd try using pool.apply while you're testing so that you see errors as soon as they occur. 如果我是你,我会在你测试时尝试使用pool.apply,这样你就会在发生错误时立即看到错误。

If you're using Python 3.2+, you can use the error_callback keyword argument to to handle exceptions raised in workers. 如果您使用的是Python 3.2+,则可以使用error_callback关键字参数来处理在worker中引发的异常。

pool.apply_async(workerfunct, args=(*args), callback=callbackfunct, error_callback=handle_error) 

handle_error will be called with the exception object as an argument. 将使用异常对象作为参数调用handle_error

If you're not, you have to wrap all your worker functions in a try / except to ensure your callback is executed. 如果不是,则必须在try / except包装所有工作函数,以确保执行callback (I think you got the impression that this wouldn't work from my answer in that other question, but that's not the case. Sorry!): (我认为你的印象是,在其他问题上我的回答不起作用,但事实并非如此。抱歉!):

def workerfunct(*args):
    try:
        # Stuff
    except Exception as e:
        # Do something here, maybe return e?

pool.apply_async(workerfunct, args=(*args), callback=callbackfunct) 

You could also use a wrapper function if you can't/don't want to change the function you actually want to call: 如果您不想/不想更改实际要调用的函数,也可以使用包装函数:

def wrapper(func, *args):
    try:
        return func(*args)
    except Exception as e:
        return e

pool.apply_async(wrapper, args=(workerfunct, *args), callback=callbackfunct) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 multiprocessing.Pool:何时使用 apply、apply_async 或 map? - multiprocessing.Pool: When to use apply, apply_async or map? 在multiprocessing中有error_callback。在Python 2中有池apply_async吗? - error_callback in multiprocessing.Pool apply_async in Python 2? multiprocessing.Pool:使用apply_async的回调选项时调用辅助函数 - multiprocessing.Pool: calling helper functions when using apply_async's callback option 如何使用 multiprocessing.Pool 判断 apply_async 函数是否已启动或仍在队列中 - How to tell if an apply_async function has started or if it's still in the queue with multiprocessing.Pool 多处理池apply_async - Multiprocessing pool apply_async 如何将multiprocessing.Pool实例传递给apply_async回调函数? - How to pass multiprocessing.Pool instance to apply_async callback function? multiprocessing.Pool().apply_async() 似乎没有运行我的函数 - multiprocessing.Pool().apply_async() doesn't seem to run my function 为什么在multiprocessing.Pool()。apply_async()中使用了多个工人? - why is more than one worker used in `multiprocessing.Pool().apply_async()`? 为什么我的 multiprocessing.Pool apply_async 只在 for 循环中执行一次 - Why is my multiprocessing.Pool apply_async only executed once inside a for loop Python多处理池apply_async错误 - Python multiprocessing pool apply_async error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM