简体   繁体   中英

Why single python process's cpu usage can be more than 100 percent?

Because of GIL, I thought a multi-thread python process can only have one thread running at one time, thus the cpu usage can not be more than 100 percent.

But I found the code bellow can occupy 950% cpu usage in top.

import threading
import time

def f():
    while 1:
        pass


for i in range(10):
    t = threading.Thread(target=f)
    t.setDaemon(True)
    t.start()

time.sleep(60)

This is not a same question as Python interpreters uses up to 130% of my CPU. How is that possible? . In that question, the OP said he was doing I/O intensive load-testing which may release the GIL. But in my program, there is no I/O operation.

Tests run on CPython 2.6.6.

One reason could be the method you're using to get to 950%. There's a number called (avg) load which is perhaps not what one would expect before reading the documentation.

The load is the (average) number of threads that's either in running or runnable state (in queue for CPU time). If you like in your example have ten busy looping threads while one thread is running the other nine is in runnable state (in queue for a time slot).

The load is an indication on how many cores that you could have made use of. Or how much CPU power your program wants to use (not necessarily the actual CPU power it gets to use).

I think in this case the CPU is busy doing thread switch instead of doing some actual work. In another word, the thread switch is using all CPUs to do its job, but the python loop code runs too fast to cause observable CPU usage. I tried adding some real calculations as below, the CPU usage dropped to around 200%. And if you add more calculations, I believe the CPU usage will be very close to 100%.

def f():
    x=1
    while 1:
        y=x*2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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