python3.13是否移除了GIL的限制

发布时间 2023-08-03 18:21:07作者: Death_Knight

 

近日看到新闻:

https://baijiahao.baidu.com/s?id=1773013936355276204&wfr=spider&for=pc

https://www.thepaper.cn/newsDetail_forward_24052522?commTag=true

 

 

 

 

 

 

 

 

 

==================================================

 

 

乍一看好像说的是Python3.13开始就不使用GIL了,异常的开心,但是细读后发现说的好像还不太是这个样子,上面说的是开始计划替代GIL了,但是可能考虑在python3.13或3.14版本中加入一些实验性的无GIL实现,并且计划在5年内逐步替代GIL。

从这个话语中我们可以知道:

1. python3.13或python3.14可能会加入一些无GIL的特性,但是也是可能,而且即使加入也只是实验阶段;

2. 虽然官方开始对GIL替换进行操作,但是计划也是要5年的时间才可以完全替代GIL,也就是说在5年时间之内GIL这个应该不太能完全的被摒弃,大家怎么也是还要继续使用GIL 5年时间。

 

 

为了一探究竟于是决定对最新版本的python进行编译,来实地看看是否没有了GIL:

 

import threading
import time
 
def print_time( threadName, delay):
    s = 0

    for i in range(1000000000):
        s+=i

    print(threadName, s)

p1=threading.Thread( target=print_time, args=("Thread-1", 2, ) )
p2=threading.Thread( target=print_time, args=("Thread-2", 4, ) )

a = time.time()
p1.run()
p2.run()
p1.join()
p2.join()
print(time.time()-a)
while True:
    pass

 

 

 

可以看到至少在Python 3.13.0a0版本中还是在继续使用GIL的。

 

 

==========================================================