python apply_async没有执行函数,Pool.apply_async():未执行嵌套函数

I am getting familiar with Python's multiprocessing module. The following code works as expected:

#outputs 0 1 2 3

from multiprocessing import Pool

def run_one(x):

print x

return

pool = Pool(processes=12)

for i in range(4):

pool.apply_async(run_one, (i,))

pool.close()

pool.join()

Now, however, if I wrap a function around the above code, the print statements are not executed (or the output is redirected at least):

#outputs nothing

def run():

def run_one(x):

print x

return

pool = Pool(processes=12)

for i in range(4):

pool.apply_async(run_one, (i,))

pool.close()

pool.join()

If I move the run_one definition outside of run, the output is the expected one again, when I'm calling run():

#outputs 0 1 2 3

def run_one(x):

print x

return

def run():

pool = Pool(processes=12)

for i in range(4):

pool.apply_async(run_one, (i,))

pool.close()

pool.join()

What am I missing here? Why isn't the second snippet printing anything? If I simply call the run_one(i) function instead of using apply_async, all the three codes output the same.

解决方案

Pool needs to pickle (serialize) everything it sends to its worker-processes. Pickling actually only saves the name of a function and unpickling requires re-importing the function by name.

For that to work, the function needs to be defined at the top-level, nested functions won't be importable by the child and already trying to pickle them raises an exception:

from multiprocessing.connection import _ForkingPickler

def run():

def foo(x):

pass

_ForkingPickler.dumps(foo) # multiprocessing custom pickler;

# same effect with pickle.dumps(foo)

run()

# Out:

Traceback (most recent call last):

...

AttributeError: Can't pickle local object 'run..foo'

The reason why you don't see an exception is, because Pool already starts catching exceptions during pickling tasks in the parent and only re-raises them when you call .get() on the AsyncResult object you immediately get when you call pool.apply_async().

That's why (with Python 2) you better always use it like this, even if your target-function doesn't return anything (still returns implicit None):

results = [pool.apply_async(foo, (i,)) for i in range(4)]

# `pool.apply_async()` immediately returns AsyncResult (ApplyResult) object

for res in results:

res.get()

Non-async Pool-methods like Pool.map() and Pool.starmap() use the same (asynchronous) low-level functions under the hood like their asynchronous siblings, but they additionally call .get() for you, so you will always see an exception with these methods.

Python 3 has an error_callback-parameter for asynchronous Pool-methods you can use instead to handle exceptions.

`pool.apply_async()` 是 Python `multiprocessing` 模块中的一个方法,用于向进程池中提交异步任务。 其语法如下: ```python apply_async(func[, args[, kwds[, callback[, error_callback]]]]) ``` 其中,`func` 是要执行的任务函数,`args` 是要传入任务函数的参数,`kwds` 是要传入任务函数的关键字参数。`callback` 是任务执行完成后要调用的回调函数,`error_callback` 是任务执行发生错误时要调用的回调函数。 `apply_async()` 方法会立即返回一个 `AsyncResult` 对象,这个对象可以用来获取任务执行的结果或者等待任务执行完成。可以使用 `get()` 方法来获取任务的结果,或者使用 `wait()` 方法等待任务执行完成。 下面是一个使用 `pool.apply_async()` 方法的示例代码: ```python import multiprocessing def worker(num): """子进程要执行的任务""" result = num * 2 return result if __name__ == '__main__': # 创建进程池 pool = multiprocessing.Pool() # 提交任务到进程池中 results = [] for i in range(5): result = pool.apply_async(worker, args=(i,)) results.append(result) # 关闭进程池 pool.close() # 等待所有进程完成任务 pool.join() # 输出结果 for result in results: print(result.get()) ``` 在这个示例中,首先创建了一个进程池 `pool`,然后使用 `apply_async()` 方法向进程池提交任务,每个任务都会调用 `worker()` 函数,计算结果并返回。在提交完所有任务后,关闭进程池并等待所有进程完成任务。最后,遍历任务结果并输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值