Python线程使用多个CPU

时间:2016-05-24 10:56:51

标签: python multithreading

我有这个实现线程的python代码。当我通过这个过程检查cpu使用情况时,似乎它使用了由于GIL而不应该出现的所有CPU。

import threading
def counter(n):
    for i in range(0,n):
        i = i+1
t1 = threading.Thread(target = counter, args = (30000000,))
t2 = threading.Thread(target = counter, args = (30000000,))
t3 = threading.Thread(target = counter, args = (30000000,))
t4 = threading.Thread(target = counter, args = (30000000,))
t1.start();t2.start();t3.start();t4.start()
t1.join();t2.join();t3.join();t4.join()

cpu使用情况统计信息:

bash@bash:~$ ps -p 3298 -L -o pid,tid,psr,pcpu
 PID   TID PSR %CPU
 3298  3298   0  0.1
 3298  3299   2 39.8
 3298  3300   3 44.8
 3298  3301   1 45.8
 3298  3302   0 50.0

有人可以解释发生了什么吗? 如果需要,lscpu命令的输出..

bash@bash:~$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 69
Stepping:              1
CPU MHz:               1700.156
BogoMIPS:              4788.99
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              3072K
NUMA node0 CPU(s):     0-3

0 个答案:

没有答案