Python GIL prevent CPU usage to exceed 100% in multiple core machine?

懵懂的女人 提交于 2019-12-23 19:51:27

问题


Many references say that, Python GIL lower down the performance of multi threading code in multi core machine, since each thread will need to acquire the GIL before executioin.

In other words, it looks like GIL make a multi threading Python program to a single thread mode in fact.

For example:

(1) Thread A get GIL, execute some time, release GIL

(2) Thread B get GIL, execute some time, release GIL

...

However, after some simple experiments, I found that although GIL lower down the performance, the total CPU usage may exceed 100% in multiple core machine.

from threading import Thread

def test():
    while 1:
        pass

for i in range(4):
    t = Thread(target=test)
    t.start()

On a 4 core, 8 thread machine, the above program will occupy around 160% CPU usage. Is there anything I misunderstand? Two threads can execute exactly at the same moment? Or the CPU usage calculation has bias or something wrong?

Thanks


回答1:


In all likelyhood, the extra 60% CPU usage you're seeing is simply your various threads fighting over the GIL.

There is, after all, some time spent outside the GIL where the interpreter is working to release/acquire the GIL and the O/S scheduler being at work to arbitrate them.




回答2:


In addition to Dolda2000 answer, Python bytecode will only be executed by a single processor at a time becuase of GIL. Only certain C modules (which don't manage Python state) will be able to run concurrently.threading is more appropriate for I/O-bound applications (I/O releases the GIL, allowing for more concurrency) in other cases, python multi-threading slower show degraded performance than serial.So to expoilt all cores and for better performance, use multiprocessing. There a very nice explanation of it in this answer, check it out!



来源:https://stackoverflow.com/questions/35081783/python-gil-prevent-cpu-usage-to-exceed-100-in-multiple-core-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!