Python multiprocessing.Pool:何时使用apply,apply_async或map? [英] Python multiprocessing.Pool: when to use apply, apply_async or map?

查看:7217
本文介绍了Python multiprocessing.Pool:何时使用apply,apply_async或map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到有关池的用例的明确示例。应用 Pool.apply_async Pool.map 。我主要使用 Pool.map ;他们的优点是什么?

I have not seen clear examples with use-cases for Pool.apply, Pool.apply_async and Pool.map. I am mainly using Pool.map; what are the advantages of others?

推荐答案

回到Python的旧时代,为了调用具有任意参数的函数,使用 apply

Back in the old days of Python, to call a function with arbitrary arguments, you would use apply:

apply(f,args,kwargs)

apply 仍然存在于Python2.7中, Python3,一般不再使用。现在,

apply still exists in Python2.7 though not in Python3, and is generally not used anymore. Nowadays,

f(*args,**kwargs)

是首选。 multiprocessing.Pool 模块尝试提供类似的界面。

is preferred. The multiprocessing.Pool modules tries to provide a similar interface.

Pool.apply 类似于Python apply ,除了函数调用是在单独的进程中执行的。 Pool.apply 阻塞,直到功能完成。

Pool.apply is like Python apply, except that the function call is performed in a separate process. Pool.apply blocks until the function is completed.

Pool.apply_async 也类似于Python的内置 apply ,除了调用立即返回,而不是等待结果。返回 ApplyResult 对象。您可以调用 get()方法来检索函数调用的结果。 get()方法阻塞,直到函数完成。因此, pool.apply(func,args,kwargs)等效于 pool.apply_async(func,args,kwargs).get code>。

Pool.apply_async is also like Python's built-in apply, except that the call returns immediately instead of waiting for the result. An ApplyResult object is returned. You call its get() method to retrieve the result of the function call. The get() method blocks until the function is completed. Thus, pool.apply(func, args, kwargs) is equivalent to pool.apply_async(func, args, kwargs).get().

Pool.apply apply_async 方法也有一个回调,如果提供的话,在函数完成时被调用。这可以用来代替调用 get()

In contrast to Pool.apply, the Pool.apply_async method also has a callback which, if supplied, is called when the function is complete. This can be used instead of calling get().

例如:

import multiprocessing as mp
import time

def foo_pool(x):
    time.sleep(2)
    return x*x

result_list = []
def log_result(result):
    # This is called whenever foo_pool(i) returns a result.
    # result_list is modified only by the main process, not the pool workers.
    result_list.append(result)

def apply_async_with_callback():
    pool = mp.Pool()
    for i in range(10):
        pool.apply_async(foo_pool, args = (i, ), callback = log_result)
    pool.close()
    pool.join()
    print(result_list)

if __name__ == '__main__':
    apply_async_with_callback()

作为

[1, 0, 4, 9, 25, 16, 49, 36, 81, 64]

注意,不像 pool.map ,结果的顺序可能不符合 pool.apply_async 调用的顺序。

Notice, unlike pool.map, the order of the results may not correspond to the order in which the pool.apply_async calls were made.

所以,如果你需要在一个单独的进程中运行一个函数,但要让当前进程阻塞,直到该函数返回,使用 Pool.apply 。像 Pool.apply Pool.map 阻塞,直到返回完整的结果。

So, if you need to run a function in a separate process, but want the current process to block until that function returns, use Pool.apply. Like Pool.apply, Pool.map blocks until the complete result is returned.

如果您希望工作进程池以异步方式执行许多函数调用,请使用 Pool.apply_async 。结果的订单无法保证与 Pool.apply_async 的调用顺序相同。

If you want the Pool of worker processes to perform many function calls asynchronously, use Pool.apply_async. The order of the results is not guaranteed to be the same as the order of the calls to Pool.apply_async.

请注意,您可以使用 Pool.apply_async 调用多个不同函数(不是所有调用都需要使用

Notice also that you could call a number of different functions with Pool.apply_async (not all calls need to use the same function).

相反, Pool.map 对许多参数应用相同的函数。
但是,与 Pool.apply_async 不同,结果以对应于参数顺序的顺序返回。

In contrast, Pool.map applies the same function to many arguments. However, unlike Pool.apply_async, the results are returned in an order corresponding to the order of the arguments.

这篇关于Python multiprocessing.Pool:何时使用apply,apply_async或map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆