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


many references that, python gil lower down performance of multi threading code in multi core machine, since each thread need acquire gil before executioin.

in other words, looks gil make multi threading python program single thread mode in fact.

for example:

(1) thread gil, execute time, release gil

(2) thread b gil, execute time, release gil

...

however, after simple experiments, found although gil lower down performance, total cpu usage may exceed 100% in multiple core machine.

from threading import thread  def test():     while 1:         pass  in range(4):     t = thread(target=test)     t.start() 

on 4 core, 8 thread machine, above program occupy around 160% cpu usage. there misunderstand? 2 threads can execute exactly @ same moment? or cpu usage calculation has bias or wrong?

thanks

in likelyhood, 60% cpu usage you're seeing various threads fighting on gil.

there is, after all, time spent outside gil interpreter working release/acquire gil , o/s scheduler being @ work arbitrate them.


Comments