-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
add asyncio guide for Free-Threaded Python #150456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kumaraditya303
wants to merge
3
commits into
python:main
Choose a base branch
from
kumaraditya303:asyncio-threadsafety
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| .. currentmodule:: asyncio | ||
|
|
||
| .. _asyncio-threading: | ||
|
|
||
| asyncio and free-threaded Python | ||
| ================================ | ||
|
|
||
| asyncio uses an event loop as a scheduler to enable highly efficient | ||
| I/O-bound concurrency by switching between tasks during non-blocking I/O | ||
| operations. It allows off-loading CPU-bound work to a thread or process | ||
| pool, but that is still limited by the :term:`global interpreter lock` | ||
| in CPython. | ||
|
|
||
| However, with :pep:`703`, Python can now be built in a "free-threaded" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's find doc references instead of PEPs to help people understand free-threading. |
||
| mode, which removes the GIL and allows true multi-threading. This means | ||
| that asyncio can now take advantage of multiple CPU cores without the | ||
| limitations imposed by the GIL. | ||
|
|
||
| Since Python 3.14, asyncio has first-class support for free-threaded | ||
| Python, and the implementation of asyncio is safe to use in a | ||
| multi-threaded environment. | ||
|
|
||
| .. seealso:: | ||
|
|
||
| `Scaling asyncio on Free-Threaded Python | ||
| <https://labs.quansight.org/blog/scaling-asyncio-on-free-threaded-python>`__, | ||
| a blog post by Kumar Aditya which explains the internal changes | ||
| that make asyncio safe and efficient under free-threaded Python, | ||
| together with benchmarks of the resulting improvements. | ||
|
|
||
|
|
||
| Thread safety considerations | ||
| ---------------------------- | ||
|
|
||
| While asyncio is designed to be thread-safe in a free-threaded Python | ||
| environment, there are still some considerations to keep in mind when | ||
| using asyncio with threads: | ||
|
|
||
| 1. **Event loop**: Each thread should have its own event loop which | ||
| should not be shared across threads. This ensures that the event loop | ||
| can manage its own tasks and callbacks without interference from | ||
| other threads. | ||
|
|
||
| 2. **Task management**: Tasks and futures created in one thread should | ||
| not be awaited or manipulated from another thread. | ||
|
|
||
| 3. **Thread-safe APIs**: When interacting with asyncio from multiple | ||
| threads, it's important to use thread-safe APIs provided by asyncio, | ||
| such as :func:`asyncio.run_coroutine_threadsafe` for submitting | ||
| coroutines to an event loop from another thread. If you need to | ||
| call a callback from a different thread, you can use | ||
| :meth:`loop.call_soon_threadsafe` to schedule it safely. | ||
|
|
||
| 4. **Synchronization**: The synchronization primitives provided by | ||
| asyncio (like :class:`asyncio.Lock` and :class:`asyncio.Event`) | ||
| are not designed to be used across threads. If you need to | ||
| synchronize between threads, you should use the synchronization | ||
| primitives from the :mod:`threading` module instead. | ||
|
|
||
|
|
||
| Using asyncio with threads | ||
| -------------------------- | ||
|
|
||
| asyncio supports running one event loop per thread, which allows you to | ||
| take advantage of multiple CPU cores in a free-threaded Python | ||
| environment. Each thread can run its own event loop, and tasks can be | ||
| scheduled on those loops independently. | ||
|
|
||
| Here's an example of how to use asyncio with threads:: | ||
|
|
||
| import asyncio | ||
| import threading | ||
|
|
||
| async def worker(name: str) -> None: | ||
| print(f"Worker {name} starting") | ||
| await asyncio.sleep(1) | ||
| print(f"Worker {name} done") | ||
|
|
||
| def run_loop(name: str) -> None: | ||
| asyncio.run(worker(name)) | ||
|
|
||
| threads = [ | ||
| threading.Thread(target=run_loop, args=(f"T{i}",)) | ||
| for i in range(4) | ||
| ] | ||
| for t in threads: | ||
| t.start() | ||
| for t in threads: | ||
| t.join() | ||
|
|
||
| In this example, each thread creates its own event loop with | ||
| :func:`asyncio.run` and runs a coroutine on it. The threads execute | ||
| concurrently, and in a free-threaded build they can run on separate | ||
| CPU cores in parallel. | ||
|
|
||
|
|
||
| Producer/consumer across threads | ||
| -------------------------------- | ||
|
|
||
| When a regular (non-asyncio) thread needs to hand work to an asyncio | ||
| event loop running in another thread, use a thread-safe primitive such | ||
| as :class:`queue.Queue` rather than :class:`asyncio.Queue`, which is | ||
| only safe within a single event loop.:: | ||
|
|
||
| import asyncio | ||
| import queue | ||
| import threading | ||
|
|
||
| def producer(q: queue.Queue[int]) -> None: | ||
| for i in range(5): | ||
| print(f"Producing {i}") | ||
| q.put(i) | ||
| q.shutdown() | ||
|
|
||
| async def consumer(q: queue.Queue[int]) -> None: | ||
| while True: | ||
| try: | ||
| item = q.get_nowait() | ||
| except queue.Empty: | ||
| await asyncio.sleep(0.1) | ||
| continue | ||
| except queue.ShutDown: | ||
| break | ||
| print(f"Consumed {item}") | ||
| await asyncio.sleep(item) | ||
|
|
||
| q: queue.Queue[int] = queue.Queue() | ||
| consumer_thread = threading.Thread( | ||
| target=lambda: asyncio.run(consumer(q)) | ||
| ) | ||
| consumer_thread.start() | ||
| producer(q) | ||
| consumer_thread.join() | ||
|
|
||
| The producer runs on the main thread while the consumer runs inside an | ||
| event loop on its own thread, yet they communicate safely through | ||
| ``queue.Queue``. When the queue is empty the consumer sleeps briefly | ||
| and tries again. When the producer is done it calls | ||
| :meth:`~queue.Queue.shutdown`, which causes subsequent | ||
| :meth:`~queue.Queue.get_nowait` calls to raise :exc:`queue.ShutDown` | ||
| so the consumer can exit cleanly. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we can say a bit more about when asyncio+threads makes sense and when it does not.