Jadiel de Armas
Jadiel de Armas

Reputation: 8802

How to adjust the number of processes of a Pool in Python

Is there a way to adjust the number of processes of a multiprocessing.Pool after it has been created? If not, what would be a correct way of implementing such functionality?

Upvotes: 1

Views: 261

Answers (1)

nathancy
nathancy

Reputation: 46680

From the source code there's a private method called _repopulate_pool() which seems to be able to change the number of workers in the pool. But I don't recommend you try to adjust the number of processors in a pool on-the-fly. You're better off just initializing the number of processors at the beginning.

To adjust the number of processes to use in multiprocessing.Pool you can use the processes flag. From the docs:

processes is the number of worker processes to use. If processes is None then the number returned by cpu_count() is used.

For instance to set the number of processes to 4, which means that the Pool class will only allow 4 processes running at the same time.

import multiprocessing as mp

pool = mp.Pool(processes=4)

Upvotes: 1

Related Questions