Unlocking the Parallel Universe: Subinterpreters and Free-Threading in Python 3.13

anthonypjshaw


1

Vote

Description:

Abstract

Python 3.12 introduced a new parallel execution model called "sub interpreters" that uses a per-interpreter GIL to unlock a new way of writing parallel code that's faster than multiprocessing. Python 3.13 introduces another new parallel execution model called "free-threading" (previously called no-gil) that replaces that optionally removes the GIL so that threads can truly be parallel. Does free-threading make sub interpreters redundant? We'll answer that question in this talk.

Overview

Python 3.12 introduced a new parallel execution model called "sub interpreters" that uses a per-interpreter GIL to unlock a new way of writing parallel code that's faster than multiprocessing. Python 3.13 introduces another new parallel execution model called "free-threading" (previously called no-gil) that replaces that optionally removes the GIL so that threads can truly be parallel. Does free-threading make sub interpreters redundant? We'll answer that question in this talk. We unpack what sub interpreters are, how to use them and their limitations. We'll explore free-threading, the optional flag to remove the GIL in 3.13. Finally we look at a use case for sub interpreters and free-threading together, a multi-worker, multi-thread ASGI web server. We benchmark this against traditional web server architecture with multiprocessing and threading. So fasten your seatbelts, fellow Pythonistas! Let’s ride the comet tail of parallelism, leaving behind the single-threaded past. 🚀✨

Outline

Hello and Introduction (2 mins) Options for parallel code execution in Python 3.12 and below (3 mins) Quick recap of concurrent and parallel definition Comparison of concurrent approaches and parallel Introduction to sub interpreters (10 mins) Creating a sub interpreter Sharing data between interpreters Pros and Cons of sub interpreters Introduction to free-threading (7 minutes) What is an optional GIL Where did all the locks go? Sharing data between free threads A practical use case (6 minutes) Converting Hypercorn to run on sub interpreters and free-threading pools Running Flask without a GIL Running FastAPI without a GIL Benchmark results Conclusion (2 minutes) Questions (remaining time)

Additional notes

This is a rapidly changing area of Python and I have been contributing to the changes in the core interpreter.

This talk is based on research from 2022-23 on benchmarking the overhead of sub interpreters as well as PRs to improve the sub interpreter API.

I gave a version of this talk at PyCon US in 2024, but if accepted for PyCon India plan to adapt it with all of the changes since May, of which there are many, including the introduction of a shared memory cache.

Prerequisites:

  • Basic understanding of parallelism
  • Basic understanding of concurrency

The outcomes of this talk are: - Attendee has a better understanding of what sub interpreters are - Attendee has a better understanding of what free-threading is - Attendee understands the pros and cons of both approaches compared with multiprocessing and traditional threading

Content URLs:

https://tonybaloney.github.io/posts/sub-interpreter-web-workers.html

Speaker Info:

Anthony Shaw is from Sydney, Australia and is a contributor to many open-source communities. Anthony maintains several popular open-source tools for DevOps, Security, Automation and Code Quality. He has been recognized for his contribution to open source, including as Fellow of the Python Software Foundation and member of the Apache Software Foundation. Anthony runs a Python blog and YouTube channel and authored the book "CPython Internals". Anthony is also a Fellow of Macquarie University and publishes articles on Python performance and compiler optimisation.

Speaker Links:

Anthony Shaw's home page includes links to talks, blog posts, GitHub projects, and publications.

Section: Core Python
Type: Talk
Target Audience: Advanced
Last Updated: