Free Threaded Python With Asyncio

submited by
Style Pass
2024-09-20 17:30:02

With the immanent release of Python 3.13, I wanted to look at the biggest changes coming to Python. I think by far the most exciting feature is free-threaded Python from PEP-703.

As I'm quite late to the party, there's already a of articles talking about it. I came accross an excellent article from Simon Wilson, which successfully demonstrated parallelism for pure Python functions. Building on top of this I wanted to look at ways to synchronise threads beyond using ThreadPoolExecutor.map.

Prior to Python 3.13 threads were used for IO bound tasks due to the GIL, Asyncio is also used for IO (duh...) and we can wrap threads using asyncio.to_thread. For example,

Note Due to the GIL, asyncio.to_thread() can typically only be used to make IO-bound functions non-blocking. However, for extension modules that release the GIL or alternative Python implementations that don’t have one, asyncio.to_thread() can also be used for CPU-bound functions. The only thing stopping us was the GIL, so CPU bound tasks shouldn't be a problem. Though it still feels a bit silly given the name AsyncIO.

The results are as expected, when we use AsyncIO to run our code concurrently we observe the speed-up we'd expect from parallel execution.

Leave a Comment