PEP 703的内容是什么,意义又是什么呢?
可以说python的官方接受的no-GIL提议的PEP就是PEP 703给出的,如果GIL帧的从python中移除那么可以说对整个python生态圈将有着跨越性发展的意义。
====================================================
PEP 703地址:
https://peps.python.org/pep-0703/
PEP 703 – Making the Global Interpreter Lock Optional in CPython
在网上找到的一个中文的nogil项目的相关内容:
Python 官方研讨会:彻底移除 GIL 真的可行么?
===========================================================
nogil项目代码地址:
https://github.com/colesbury/nogil
https://hub.docker.com/r/nogil/python-cuda
为了测试nogil项目的效果,于是手动编译nogil-python3.9,并和新编译的python3.13进行性能对比:
nogil项目的编译命令:
./configure [--prefix=PREFIX] [--enable-optimizations] make -j make install
测试代码:
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.start() p2.start() p1.join() p2.join() print(time.time()-a)
测试结果:
可以看到nogil项目的多核心多线程确实可以较大程度提高多线程性能。
-----------------------------------------------------
再使用nogil项目的官方测试代码:
import sys from concurrent.futures import ThreadPoolExecutor print(f"nogil={getattr(sys.flags, 'nogil', False)}") def fib(n): if n < 2: return 1 return fib(n-1) + fib(n-2) threads = 8 if len(sys.argv) > 1: threads = int(sys.argv[1]) with ThreadPoolExecutor(max_workers=threads) as executor: for _ in range(threads): executor.submit(lambda: print(fib(34)))
python3.13的表现:
(base) root@75f68d8ff029:~/nogil# time /root/python3.13/bin/python3.13 /tmp/x2.py
nogil=False
9227465
9227465
9227465
9227465
9227465
9227465
9227465
9227465
real 0m5.623s
user 0m5.638s
sys 0m0.004s
(base) root@75f68d8ff029:~/nogil# time /root/python3.13/bin/python3.13 /tmp/x2.py
nogil=False
9227465
9227465
9227465
9227465
9227465
9227465
9227465
9227465
real 0m5.608s
user 0m5.618s
sys 0m0.012s
(base) root@75f68d8ff029:~/nogil# time /root/python3.13/bin/python3.13 /tmp/x2.py
nogil=False
9227465
9227465
9227465
9227465
9227465
9227465
9227465
9227465
real 0m5.602s
user 0m5.591s
sys 0m0.028s
------------------------------------------
nogil python3.9的表现(8线程):
(base) root@75f68d8ff029:~/nogil# time /root/python3.9_nogil/bin/python3.9 /tmp/x2.py
nogil=True
9227465
9227465
9227465
9227465
9227465
9227465
9227465
9227465
real 0m0.506s
user 0m3.816s
sys 0m0.012s
(base) root@75f68d8ff029:~/nogil# time /root/python3.9_nogil/bin/python3.9 /tmp/x2.py
nogil=True
9227465
9227465
9227465
9227465
9227465
9227465
9227465
9227465
real 0m0.514s
user 0m3.882s
sys 0m0.004s
(base) root@75f68d8ff029:~/nogil# time /root/python3.9_nogil/bin/python3.9 /tmp/x2.py
nogil=True
9227465
9227465
9227465
9227465
9227465
9227465
9227465
9227465
real 0m0.502s
user 0m3.815s
sys 0m0.004s
可以看到python3.9_nogil项目对多线程的代码在no-GIL情况下确实可以得到非常好的效果。
=============================================
标签:python3.13,no,PEP,time,print,nogil,GIL,root,python3.9 From: https://www.cnblogs.com/devilmaycry812839668/p/17604295.html