简体   繁体   English

python multiprocessing,cpu-s和cpu内核

[英]python multiprocessing, cpu-s and cpu cores

I was trying out python3 multiprocessing on a machine that has 8 cpu-s and each cpu has four cores (information is from /proc/cpuinfo ). 我正在具有8个cpu-s的计算机上尝试python3 multiprocessing ,每个cpu具有四个核心(信息来自/proc/cpuinfo )。 I wrote a little script with a useless function and I use time to see how long it takes for it to finish. 我写了一个带有无用功能的小脚本,花time看完成它需要多长时间。

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)

Calling f() without multiprocessing takes about 7s ( time 's "real" output). 不进行多处理就调用f()大约需要7秒钟( time的“实际”输出)。 Calling f() 8 times with a pool of 8 as seen above, takes around 7s again. 如上所示,使用8的池调用f() 8次,大约需要7秒钟。 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. 如果我用4个池调用8次,则得到的时间约为13.5s,因此启动脚本会有一些开销,但运行时间是原来的两倍。 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 f() 32 times on a pool of 8. 如果有8个cpu-s每个都有4个内核,如果我用32个池调用32次,据我所知它应该再次运行7s左右,但实际上需要32s,这比运行f()在8池比赛中32次。

So my question is multiprocessing not able to make use of cores or I don't understand something about cores or is it something else? 因此,我的问题是multiprocessing无法利用内核,或者我对内核不了解,还是其他?

Simplified and short.. Cpu-s and cores are hardware that your computer have. 简化且简短。.Cpu-s和内核是计算机拥有的硬件。 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. 在计算机上运行的程序分配有cpu时间。 One of these programs is the python interpetar, which runs all the programs that has the endswith .py. 这些程序之一是python interpetar,它运行所有以.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. 因此,在您计算机上的CPU时间中,时间分配给了python3。*,这反过来又将时间分配给了您正在运行的程序。 This speed will depend on what hardware you have, what operation you are running, and how cpu-time is allotted between all these instances. 此速度将取决于您拥有的硬件,正在运行的操作以及在所有这些实例之间分配cpu时间的方式。

How is cpu-time allotted? 如何分配CPU时间? 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. 这就像一个while循环,OS在程序之间分配增量,而python解释器将增量分配时间分配给python解释器运行的程序。 This is the reason the entire computer halts when a program misbehaves. 这就是程序行为异常时整个计算机停止运行的原因。

Many processes does not equal more access to hardware. 许多进程并不等于对硬件的更多访问。 It does equal more allotted cpu-time from the python interpretor allotted time. 它确实等于python解释器分配的时间所分配的cpu时间。 Since you increase the number of programs under the python interpretor which do work for your application. 由于您增加了python解释器下对您的应用程序有效的程序的数量。

Many processes does equal more work horses. 许多过程等于更多的工作量。


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. 您将主力工作的数量增加到分配给cpu-time的python解释器在如此多的进程之间分配的程度,以至于所有进程都变慢了。

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

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