離跟 GIL 說再見更進一步的 Free-threaded CPython

在「Free-threaded CPython is ready to experiment with (quansight.org)」這邊看到 PythonGIL 的進度,從原文「Free-threaded CPython is ready to experiment with!」這邊可以看到有些需要套件一起配合的,像是 NumPy 這邊的「BUG: error Python 3.13 free-threaded RuntimeError: Identity cache already includes the item.」。

看起來用到 C 的實作 (大多數應該是為了加速,或是需要底層的 syscall) 都會需要注意一下。

另外值得一提的是目前拔掉 GIL 的版本在 single threading 的情況下會比標準版的 Python 3 慢很多,會需要後續再改進,所以一開始的版本就算穩定下來也可能沒有經濟上的效益,參考 id=40949628 這邊提到的:

Right now there is a significant single-threaded performance cost. Somewhere from 30-50%. Part of what my colleague Ken Jin and others are working on is getting back some of that lost performance by applying some optimizations. Expect single-threaded performance to improve for Python 3.14 next year.

另外有看到一些有趣的討論,像是為什麼不 bump 版本號碼到 Python 4 (因為語法沒有大改變?)。

透過環境變數拿掉 Python 的 GIL 限制 (目前會爛掉一堆東西)

Hacker News 上看到「gh-116167: Allow disabling the GIL (github.com/python)」這個討論,原連結在「gh-116167: Allow disabling the GIL with PYTHON_GIL=0 or -X gil=0 #116338」這邊。

可以看到 Python 提供透過環境變數拿掉 GIL 的方式,讓大家可以更方便測試。

要注意的是這就只是「拿掉 GIL」,沒有說東西會正常 (噗):

Trying to run the full test suite crashes pretty quickly, in test_asyncio.

但這讓其他人可以更方便測試,算是很不錯的進展...

Python 的原生 multithreading 支援

Simon Willison 這邊看到的:「Real Multithreading is Coming to Python - Learn How You Can Use It Now」,他引用的原文在「Real Multithreading is Coming to Python - Learn How You Can Use It Now」這邊,在講 Python 3.12 將會有原生 multithreading 支援。

Python 裡知名的 GIL 問題將會用 Per-Interpreter GIL 的技術來解決,把 GIL 的 global-based 改寫變成 thread-based:

With introduction of "Per-Interpreter GIL", individual Python interpreters don't share the same GIL anymore. This level of isolation allows each of these sub-interpreters to run really concurrently.

這算是基礎建設,之後應該會有蠻長的陣痛期要轉換,尤其是各個其他程式語言寫的 library 要考慮到 thread-safe 的問題。

話說回來,PHP 就沒繼續討論過 threading 這個問題了,大家還是繼續用 process 架構在搞 XD

Google 弄出來的 Grumpy:把 Python 2.7 的程式碼轉成 Go...

Google 放出 Grumpy,可以把 Python 2.7 的程式碼轉成 Go:「Grumpy: Go running Python!」。

下面看到一個留言頗有趣的:

This sad to see that Grumpy is mean to be a replacement of CPython 2.7 instead of CPython 3.x . I presume the code from youtube was written in python 2.x hence the reason but I hope we'll see Grumpy supporting python 3.x :)

回到原文,這次的需求主要是出自 YouTube 的需求:

The front-end server that drives youtube.com and YouTube’s APIs is primarily written in Python, and it serves millions of requests per second! YouTube’s front-end runs on CPython 2.7, so we’ve put a ton of work into improving the runtime and adapting our application to work optimally within it.

然後 Python 的 GIL 又被拿出來鞭屍:

除了 C extension 不支援外,還是有些「過於動態」的語法不支援:

exec, eval and compile: These dynamic features of CPython are not supported by Grumpy because Grumpy modules consist of statically compiled Go code. Supporting dynamic execution would require bundling Grumpy programs with the compilation toolchain which would be unwieldy and impractically slow.

這樣可用的範圍少不少,這個專案可以當作 YouTube 這種規模的網站所做的改善,而不是什麼可以拿來用的工具 :o