函数apply_async 在Python 3 中具有参数error_callback.但是Python 2 中没有这个论点.
Function apply_async of multiprocessing.Pool class has argument error_callback in Python 3. But this argument is missing in Python 2.
有什么技巧可以在 Python 2 中实现相同的功能吗?理想情况下,我想编写在 Python 2 和 3 中运行的代码.
Is there any trick to achieve the same functionality in Python 2 ? Ideally I would like to write code which runs in both Python 2 and 3.
我还没试过python3.但对我来说,为了捕捉子进程中的错误,我将在子进程中运行的函数放在一个
I haven't tried python3 yet. But for me, to catch the errors in the child process, I put the function that runs in child process within a
import traceback
try:
your code that can make error
except Exception as e:
print e
return False, traceback.format_exc()
else:
return True, result
这样我会知道是否有问题.
So that I will know if something goes wrong.
我将返回格式更改为 OP 的注释,以便子进程返回一个元组 (is_success, result or error traceback message)
I change the return format as OP's comment so that the child process returns a tuple (is_success, result or error traceback message )
这样主进程将首先读取标志is_success,然后相应地处理第二个参数.
So that main process will first read the flag is_success and then handles the second argument accordingly.
这篇关于多处理中的error_callback.Python 2中的池apply_async?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Python 多处理模块的 .join() 方法到底在做什么?What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多处理模块的 .join() 方法到底在做什么?)
在 Python 中将多个参数传递给 pool.map() 函数Passing multiple parameters to pool.map() function in Python(在 Python 中将多个参数传递给 pool.map() 函数)
multiprocessing.pool.MaybeEncodingError: 'TypeError("multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEnc
Python 多进程池.当其中一个工作进程确定不再需要Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多进程池.当其中一
如何将队列引用传递给 pool.map_async() 管理的函数How do you pass a Queue reference to a function managed by pool.map_async()?(如何将队列引用传递给 pool.map_async() 管理的函数?)
与多处理错误的另一个混淆,“模块"对象没yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(与多处理错误的另一个混淆,“模块对象