Python GIL是否可以防止多核计算机上的CPU使用率超过100%? [英] Python GIL prevent CPU usage to exceed 100% in multiple core machine?

查看:303
本文介绍了Python GIL是否可以防止多核计算机上的CPU使用率超过100%?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多参考文献指出,Python GIL降低了多核计算机中多线程代码的性能,因为每个线程都需要在执行之前获取GIL.

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.

换句话说,看起来GIL实际上将多线程Python程序制作为单线程模式.

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

例如:

(1)线程A获取GIL,执行一段时间,释放GIL

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

(2)线程B获得GIL,执行一段时间,释放GIL

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

...

但是,经过一些简单的实验,我发现尽管GIL降低了性能,但多核计算机的总CPU使用率可能超过100%.

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()

在4核8线程计算机上,以上程序将占用大约160%的CPU使用率. 我有什么误会吗?两个线程可以完全同时执行 ?还是 CPU使用率计算有偏差或出了什么问题?

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?

谢谢

推荐答案

在很可能的情况下,您看到的60%以上的CPU使用率仅仅是争夺GIL的各种线程.

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

毕竟,在GIL外面花了一些时间,解释器正在努力释放/获取GIL,而O/S调度程序正在工作以对其进行仲裁.

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.

这篇关于Python GIL是否可以防止多核计算机上的CPU使用率超过100%?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆