2025-03-01 –, SS 117
Part of the reason why we would want to write Rust code for a Python library is for speed, another is to unlock the power of multithreading. In this workshop, we will look into advanced topics in PyO3 regarding support async in Python and how to do multithreading with PyO3.
Prerequisite
Please note that in this workshop, besides assuming that attendees have experience programming async in Python, attendees are required to have basic knowledge of Rust. Attendees are highly advised to go over the Rust Book to make sure they understand coding in Rust. Attendees are also advised to be already familiar with PyO3. If they are new to PyO3, first doing the PyO3 101 workshop would be ideal.
Preflight checklist
- Install/ Update Rust
- Make sure having Python 3.13, both the "normal" version (with GIL) and free-threaded Python)
- Make sure using virtual environment (recommend using uv)
Windows checklist
In this workshop we recommend using Unix OS (Mac or Linux). If you use Windows, you may encounter problems with Rust and Maturin. To minimise issues that you may encounter, please go through the extra checklist below:
Setting up
- Create a new working directory
mkdir pyo3_202
cd pyo3_202
- Set up virtual environment and install maturin
uv venv .venv
source .venv/bin/activate
uv pip install maturin
python -m ensurepip --default-pip
Note: the last command is needed as maturin develop
cannot find pip otherwise
Target audience
Developers who would like to create powerful Python libraries that can support async in Python and speed up tasks with multithreading.
Goal
Deepend attendees' knowledge in PyO3, and strengthen understanding of parallelism in Python and Rust.
Outline
Part 1 - introduction and revision of PyO3 (30 mins)
- a recap of what we learned in PyO3 101
- about async and multithreading in Python and in Rust
15 mins break + Q&A
Part 2 - Support async and await in Python (60 mins)
- creating Python async functions with PyO3 (some coding exercises)
- troubleshooting for async (some coding exercises)
15 mins break + Q&A
Part 3 - Multithreading (90 mins)
- introduction of multithreading in Rust (some coding exercises)
- multithreading in PyO3 (some coding exercises)
- Free-threaded CPython and benchmarking (some coding exercises)
- Thread safety (discussion)
2 hours
Category:Advanced and Core Python
Audience Level:Advanced
After having a career as a Data Scientist and Developer Advocate, Cheuk dedicated her work to the open-source community. Currently, she is working as AI developer advocate for JetBrains. She has co-founded Humble Data, a beginner Python workshop that has been happening around the world. She has served the EuroPython Society board for two years and is now a fellow and director of the Python Software Foundation.