PyCon US 2024 Talk Notes - Unlocking the Parallel Universe: Sub Interpreters and Free-Threading in Python 3.13
- PyCon 2023 – Eric Snow talk on sub interpreters
- EuroPython 2022 – Sam Gross talk on free-threading
- PyCon 2024 - “Sync vs Async in Python” happening right now
- PyCon 2024 - Building a JIT compiler for Cpython
- PyCon 2024 – Overcoming GIL with sub interpreters and immutability
- “Parallelism and Concurrency” chapter from CPython Internals
- My Masters Thesis doi.org/10.25949/23974764.v1
Model | Execution | Start-up time | Data Exchange | Best for… |
---|---|---|---|---|
threads | Parallel * | small | Any | Small, IO-bound tasks that don’t require multiple CPU cores |
coroutines | Concurrent | smallest | Any | Small, IO-bound tasks that don’t require multiple CPU cores |
multiprocessing | Parallel | large | Serialization | Larger, CPU or IO-bound tasks that require multiple CPU cores |
Sub Interpreters | Parallel | medium** | Serialization or Shared Memory | Larger, CPU or IO-bound tasks that require multiple CPU cores |
Crude sample:
import numpy
# Create a random array of 100,000 integers
a = numpy.random.randint(0, 100, 100_000)
for x in a:
abs(x - 50)
Benchmark code to get the "2x slower" figure:
import numpy
import threading
# Create a random array of 100,000 integers between 0 and 100
a = numpy.random.randint(0, 100, 100_000)
def simple_abs_range(vec):
for x in vec:
abs(x - 50)
def f_linear():
# Calculate the distance for each value to 50
simple_abs_range(a)
def f_threaded():
threads = []
# Split array into blocks of 100 and start a thread for each
for ar in numpy.split(a, 100):
t = threading.Thread(target=simple_abs_range, args=(ar,))
t.start()
threads.append(t)
for t in threads:
t.join()
The Jupyter Notebook for this sample is here.
The demo code is here
- Specializations are not enabled in free threading (yet)
- GitHub Actions does not have a free-threaded build 1
- Packaging does not detect the ABI for free-threading correctly 1
- Some benchmarks are slower with free threading
- The datetime module is not thread safe 1
- C Extensions need to support multi-phase-init to be supported with sub interpreters 1
- Most of your 3rd party C extensions aren’t supported yet 1
- Cython is not supported 1 2
- Django does not work in sub interpreters because of (5)
- Did I mention the datetime module is not thread safe
- Most PyPi C extensions are not thread safe
- Orjson, pydantic-core, httptools and uvloop don’t compile
Nothing using PY03 is supported 1https://github.com/PyO3/pyo3/releases/tag/v0.22.0