简体   繁体   English

multiprocessing Pool(processes=n) 如何处理过多的 Pool 请求?

[英]How does multiprocessing Pool(processes=n) handle excessive Pool requests?

Assume I have code like the following--where I need to run a function (with different params) x times.假设我有如下代码——我需要运行一个函数(使用不同的参数)x 次。 However, my Pool count is less--eg x/2.但是,我的池数较少——例如 x/2。

args_list = []

# Range(8) is just an example. My args are more complex.
for r in Range(8):
    args_list.append(r)

with Pool(4) as proc_pool:
    results = proc_pool.map(my_func, args_list)
    proc_pool.close()
    proc_pool.join()

Will Pool only try to process 4 at a time--then move on to the next 4, or will all 8 be processed at once--but only in 4 Pools? Pool只尝试处理 4 个——然后继续处理下一个 4,或者是否会一次处理所有 8 个——但只在 4 个池中处理?

If Pool will try to process all 8 in 4 Pools at once, what is the best way to handle this?如果 Pool 将尝试一次处理 4 个池中的所有 8 个,那么处理此问题的最佳方法是什么? (I can put the with Pool code in a loop to only use 4 Pools at once.) (我可以将with Pool代码放入循环中,以便一次仅使用 4 个池。)

I read the documentation, but it was not clear to me.我阅读了文档,但我不清楚。

The number passed in Pool 's first argument is the number of worker processes in the pool (in this case 4). Pool的第一个参数中传递的数字是池中工作进程的数量(在本例中为 4)。 The map function will run on each argument. map 函数将在每个参数上运行。 Each time a worker finished it's available to be used to run another argument.每次工作完成时,它都可用于运行另一个参数。

To illustrate this, consider the following:为了说明这一点,请考虑以下事项:

import time
def my_func(r):
   if r == 1:
       time.sleep(120)
   return r * r

The first thing that will happen is that 4 runs will be sent the workers.将发生的第一件事是将向工作人员发送 4 次运行。 All of them will finish almost immediately, except for the one which r == 1 .除了r == 1之外,所有这些几乎都会立即完成。 As the workers finish, the worker is re-used for another input.当工人完成时,工人被重新用于另一个输入。 So, in the example, 7 of the workers will finish almost immediately but the last one will take about 2 minutes.因此,在示例中,7 个工人几乎会立即完成,但最后一个需要大约 2 分钟。 Since the map function will wait until all workers finish to return the results, the map function will take 2 minutes to finish.由于 map 函数会等到所有 worker 完成返回结果,map 函数将需要 2 分钟才能完成。

To give another example:再举一个例子:

import time
def my_func(r):
   if r in (1, 3, 5, 7):
       time.sleep(120)
   return r * r

Half of the runs will complete almost instantly, where 4 of the runs will take 2 minutes.一半的运行将几乎立即完成,其中 4 次运行需要 2 分钟。 If five of the runs would take 2 minutes (say for r in (1, 2, 3, 5, 7) ), the total time would be 4 minutes, since for 2 minutes 4 processes would be waiting and for 2 minutes 1 process would be waiting.如果五次运行需要 2 分钟(比如r in (1, 2, 3, 5, 7) ),总时间将为 4 分钟,因为 2 分钟 4 个进程将等待 2 分钟 1 个进程会等待。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM