When using multiprocessing Pool should the number of worker processes be the same as the number of CPU's or cores?

2.3k views Asked by At

When using Python multiprocessing Pool should the number of worker processes be the same as the number of CPU's or cores?

This article http://www.howtogeek.com/194756/cpu-basics-multiple-cpus-cores-and-hyper-threading-explained/ says each core is really a central processing unit on a CPU chip. And thus it seems like there should not be a problem with having 1 process/core

e.g. If I have a single CPU chip with 4 cores, can 1 process/core for a total of 4 processes be ran without the possibility of slowing performance.

2

There are 2 answers

0
z0rberg's On

From what I've learned regarding python and multiprocessing, the best course of action is...

  • One process per core, but skip logical ones.

Hyperthreading is no help for python. It'll actually hurt performance in many cases, but test it yourself first of course.

  • Use the affinity (pip install affinity) module to stick each process to a specific core.

At least tested extensively on windows using 32bit python, not doing this will hurt performance significantly due to constant trashing of the cache. And again: skip logical cores! Logical ones, assuming you have an intel cpu with hyperthreading, are 1,3,5,7, etc.

More threads than real cores will help you nothing, unless there's also IO happening, which it shouldn't if you're crunching numbers. Test my claim yourself, especially if you use Linux, as I didn't get to test in Linux at all.

2
noxdafox On

It really depends on your workload. Case by case, the best approach is to run some benchmark test and see what is the result.

Scheduling processes is an expensive operation, the more running processes, the more you need to change context.

If most of your processes are not running (they are waiting for IO for example) then overcommitting might prove beneficial. On the opposite, if your processes are running most of the time, adding more of them contending your CPU is going to be detrimental.