简体   繁体   English

Python 多处理:如何添加或更改池中的进程数

[英]Python Multiprocessing: How to add or change number of processes in a pool

I have created a pool from the python multiprocessing module and would like to change the number of processes that the pool has running or add to them.我已经从 python 多处理模块创建了一个池,并想更改池中正在运行的进程数或将其添加到其中。 Is this possible?这可能吗? I have tried something like this (simplified version of my code)我试过这样的事情(我的代码的简化版本)

class foo:
    def __init__():
        self.pool = Pool()
    def bar(self, x):
        self.pool.processes = x
        return self.pool.map(somefunction, list_of_args)

It seems to work and achieves the result I wanted in the end (which was to split the work between multiple processes) but I am not sure that is this the best way to do it, or why it works.它似乎有效并最终实现了我想要的结果(即在多个进程之间拆分工作)但我不确定这是最好的方法,或者它为什么有效。

I don't think this actually works:我认为这实际上行不通:

import multiprocessing, time

def fn(x):
    print "running for", x
    time.sleep(5)

if __name__ == "__main__":
    pool = multiprocessing.Pool()
    pool.processes = 2

    # runs with number of cores available (8 on my machine)
    pool.map(fn, range(10))

    # still runs with number of cores available, not 10
    pool.processes = 10
    pool.map(fn, range(10))

multiprocessing.Pool stores the number of processes in a private variable (ie Pool._processes ) which is set at the point when the Pool is instantiated. multiprocessing.Pool将进程数存储在私有变量(即Pool._processes )中,该变量是在实例化 Pool 时设置的。 See the source code .查看源代码

The reason this appears to be working is because the number of processes is automatically set to the number of cores on your current machine unless you specify a different number.这似乎有效的原因是因为进程数自动设置为当前机器上的核心数,除非您指定不同的数字。

I'm not sure why you'd want to change the number of processes available -- maybe you can explain this in more detail.我不确定您为什么要更改可用进程的数量——也许您可以更详细地解释一下。 It's pretty easy to create a new pool though whenever you want (presumably after other pools have finished running).尽管您可以随时创建一个新池(大概是在其他池完成运行之后),但创建一个新池非常容易。

You can by using the private variable _processes and private method _repopulate_pool .您可以使用私有变量_processes和私有方法_repopulate_pool But I wouldn't recommend using private variables etc.但我不建议使用私有变量等。

pool = multiprocessing.Pool(processes=1, initializer=start_process)
>Starting ForkPoolWorker-35

pool._processes = 3
pool._repopulate_pool()
>Starting ForkPoolWorker-36
>Starting ForkPoolWorker-37

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

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