Real Concurrency in Python 3.14: InterpreterPoolExecutor and Beyond

前言 / Introduction 隨著 Python 3.14 的正式發布,多線程開發的限制,也隨著下面的改動迎刃而解: Free-threaded mode (PEP 703), Isolated interpreters (PEP 684), And the new InterpreterPoolExecutor (PEP 734), Python can now run true parallel CPU-bound code directly — even within the same process. 下面將展示一些 Example Async I/O CPU-bound parallelism Multicore execution with interpreter pools 1. Classic Problem: Async + CPU Work 如果你遇到需要 You fetch data from multiple APIs (I/O-bound) Then process, analyze, or compress the data (CPU-bound) 在 Python 3.14 之前 ...

September 29, 2025 · 3 min · 558 words · Daniel Ho

The GIL Problem and What Changes in Python 3.13+

前言 / Introduction 我們在上一篇講述了如何有效的處理 I/O bound 的方法,但實際上困擾 Python 開發的問題在於 CPU bound,原因來自 Global Interpreter Lock (GIL)。 這項限制導致 CPU-bound work slow,迫使許多開發者放棄Python,轉而使用其他語言去進行開發。 但這將會是過去式! 從 Python 3.12,尤其是 3.13 and 3.14,Python 進行大量的改動,使其能夠有效的開發多線程: Multiple interpreters running in parallel (PEP 684, PEP 734) Free-threaded mode (PEP 703) New executors like InterpreterPoolExecutor Less performance penalty compared to past experimental builds 在這篇文章將簡短討論 why the GIL was a bottleneck ,過去的情況是如何,以及有甚麼改變。 What the GIL Really Does The Global Interpreter Lock ensures that only one thread executes Python bytecode at a time — even on a multicore machine. ...

September 29, 2025 · 3 min · 573 words · Daniel Ho

Asyncio vs Threads: Avoiding I/O Blocking in Modern Python

前言 / Introduction 在 Python 專案的開發過程,或多或少會碰到需要平行運行的時候,而大多的資料只說使用 asyncio 就可以加速,但實際上執行時,卻還是一樣的緩慢甚至更糟,那或許你也踩到了錯誤使用線程的坑。 在這篇文章,將討論下面幾點: Why I/O-bound and CPU-bound tasks behave differently The right way to use asyncio Why requests.get() blocks async functions- This sets the foundation for understanding Python 3.13 and 3.14 improvements later in the series. I/O-Bound vs CPU-Bound: The Real Difference 在討論 asyncio or threads 之前,我們需要先搞清楚甚麼樣的任務類型是你面對的 Type What slows it down? Best approach I/O-bound Waiting on network, files, DB, API asyncio, threads CPU-bound Calculations, parsing, encoding multiprocessing, free-threading If your app is fetching APIs, reading files, or handling sockets → it’s I/O-bound. If it’s crunching data or doing math → it’s CPU-bound. 本篇文章為了更清楚描述,接下來會專注在 I/O-bound 的討論。 ...

September 29, 2025 · 2 min · 362 words · Daniel Ho