python multiprocessing, cpu-s and cpu cores
<p>I was trying out <code>python3</code> <code>multiprocessing</code> on a machine that has 8 cpu-s and each cpu has four cores (information is from <code>/proc/cpuinfo</code>). I wrote a little script with a useless function and I use <code>time</code> to see how long it takes for it to finish.</p>

<pre><code>from multiprocessing import Pool,cpu_count
def f(x):
    for i in range(100000000):
        x*x
    return x*x
with Pool(8) as p:
    a = p.map(f,[x for x in range(8)])
#~ f(5)
</code></pre>

<p>Calling <code>f()</code> without multiprocessing takes about 7s (<code>time</code>'s "real" output). Calling <code>f()</code> 8 times with a pool of 8 as seen above, takes around 7s again. If I call it 8 times with a pool of 4 I get around 13.5s, so there's some overhead in starting the script, but it runs twice as long. So far so good. Now here comes the part that I do not understand. If there are 8 cpu-s each with 4 cores, if I call it 32 times with a pool of 32, as far as I see it should run for around 7s again, but it takes 32s which is actually slightly longer than running <code>f()</code> 32 times on a pool of 8.</p>

<p>So my question is <code>multiprocessing</code> not able to make use of cores or I don't understand something about cores or is it something else?</p>

<p>Simplified and short.. Cpu-s and cores are hardware that your computer have. On this hardware there is a operating system, the middleman between hardware and the programs running on the computer. The programs running on the computer are allotted cpu time. One of these programs is the python interpetar, which runs all the programs that has the endswith .py. So of the cpu time on your computer, time is allotted to python3.* which in turn allot time to the program you are running. This speed will depend on what hardware you have, what operation you are running, and how cpu-time is allotted between all these instances. </p>

<p><strong>How is cpu-time allotted?</strong> It is an like an while loop, the OS is distributing incremental between programs, and the python interpreter is incremental distributing it's distribution time to programs runned by the python interpretor. This is the reason the entire computer halts when a program misbehaves.</p>

<p><strong>Many processes does not equal</strong> more access to hardware. It does equal more allotted cpu-time from the python interpretor allotted time. Since you increase the number of programs under the python interpretor which do work for your application. </p>

<p><strong>Many processes does equal</strong> more work horses. </p>

<hr>

<p>You see this in practice in your code. You increase the number of workhorses to the point where the python interpreters allotted cpu-time is divided up between so many processes that all of them slows down.  </p>

python multiprocessing