使用多个cpu的Python线程

2024-03-29 05:30:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个实现线程的python代码。当我检查这个进程的cpu使用情况时,它似乎使用了所有的cpu,而这不应该是由于GIL造成的。你知道吗

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

Tags: bashcachetargetcounterargscputhreadstart