简体   繁体   English

python多处理和核心数

[英]python multiprocessing and number of cores

I have a mac , and it has 2 physical cores and 4 logical cores.我有一个 mac ,它有 2 个物理内核和 4 个逻辑内核。 I am trying to figure out the python multiprocessing .我想弄清楚 python multiprocessing 。 As far as I know, each process in the processing uses one core in the computer, so I believe the number of processes in a python script can not exceed the number of the core of computer.据我所知,处理中的每个进程都使用计算机的一个内核,所以我相信python脚本中的进程数不能超过计算机的内核数。 Is this correct ?这样对吗 ?

But if I execute the following code (it has 8 functions ) , it runs same time with the version that has 4 functions.但是如果我执行以下代码(它有 8 个函数),它会与具有 4 个函数的版本同时运行。

from multiprocessing import Process
import time 
def func1():
  for i in range(0,500):
    print "11111"
    time.sleep(0.1)


def func2():
  for i in range(500,1000):
    print "22222"
    time.sleep(0.1)

def func3():
  for i in range(1000,1500):
    print "33333"
    time.sleep(0.1)


def func4():
   for i in range(1500,2000):
    print "444444"
    time.sleep(0.1)

def func5():
   for i in range(2000,2500):
    print "555555"
    time.sleep(0.1)


def func6():
   for i in range(2500,3000):
    print "666666666"
    time.sleep(0.1)


def func7():
  for i in range(3500,4000):
    print "7777777777"
    time.sleep(0.1)

def func8():
  for i in range(4500,5000):
    print "8888888888 "
    time.sleep(0.1)

if __name__=='__main__':
  start_time = time.time()
  p1 = Process(target = func1)
  p1.start()
  p2 = Process(target = func2)
  p2.start()
  p3 = Process(target = func3)
  p3.start()
  p4 = Process(target = func4)
  p4.start()
  p5 = Process(target = func5)
  p5.start()
  p6 = Process(target = func6)
  p6.start()
  p7 = Process(target = func7)
  p7.start()
  p8 = Process(target = func8)
  p8.start()
  p1.join()
  p2.join()
  p3.join()
  p4.join()
  p5.join()
  p6.join()
  p7.join()
  p8.join()
  print "done"
  print("--- %s seconds ---" % (time.time() - start_time))

If I run this code with 4 functions or 8 functions, the execution time is same, but the number of cores is 4, so I think the 8 functions version should take longer, but it did not.如果我用4个函数或8个函数运行这段代码,执行时间是一样的,但核心数是4个,所以我认为8个函数版本应该需要更长的时间,但事实并非如此。 I am missing something here ?我在这里遗漏了什么?

Thank you !谢谢 !

You can start as many processes from your Python script as you like and the operating system scheduler will schedule them to run on your CPU cores.您可以根据需要从 Python 脚本启动任意数量的进程,操作系统调度程序将安排它们在您的 CPU 内核上运行。 If those processes are CPU bound, then you won't get better performance by starting more processes than cores.如果这些进程受 CPU 限制,那么通过启动比内核更多的进程,您将无法获得更好的性能。 But in your example, the processes are spending most of their time sleeping, and therefore they aren't competing for the cores, which is why 8 processes can run in the same time as 4.但是在您的示例中,进程大部分时间都在休眠,因此它们不会竞争内核,这就是为什么 8 个进程可以与 4 个进程同时运行的原因。

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

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