Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 409
WKPlus Avatar asked Mar 04 '16 08:03

WKPlus


People also ask

Can a process use more than 100% CPU?

If the CPU usage is around 100%, this means that your computer is trying to do more work than it has the capacity for. This is usually OK, but it means that programs may slow down a little. Computers tend to use close to 100% of the CPU when they are doing computationally-intensive things like running games.

Why does top show more than 100 CPU?

The reason behind that is the CPU usage represents the usage of each core, which means, 100% means the usage of 1 core, 200% means it's occupying two cores. In short, it is CPU usage percentage per core in top command, we can use i to toggle this behaviour.

How do I limit CPU usage in Python?

Limiting CPU and Memory Usage Resources like CPU, memory utilised by our Python program can be controlled using the resource library. To get the processor time (in seconds) that a process can use, we can use the resource. getrlimit() method. It returns the current soft and hard limit of the resource.


1 Answers

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
like image 87
Huajun Zeng Avatar answered Oct 13 '22 00:10

Huajun Zeng