]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-129898: per-thread current task implementation in asyncio (#129899)
authorKumar Aditya <kumaraditya@python.org>
Wed, 19 Feb 2025 16:34:49 +0000 (22:04 +0530)
committerGitHub <noreply@github.com>
Wed, 19 Feb 2025 16:34:49 +0000 (16:34 +0000)
commit660f126f870535b6fa607e6d9cdd3cdbd9ed2cb1
tree3c37bf428ac66f977f865236434758ad57d05a76
parentfb17f41522718013036ce44cbe83a72f5d9a2104
gh-129898: per-thread current task implementation in asyncio (#129899)

Store the current running task on the thread state, it makes it thread safe for the free-threading build and while improving performance as there is no lock contention, this effectively makes it lock free.
When accessing the current task of the current running loop in current thread, no locking is required and can be acessed without locking.
In the rare case of accessing current task of a loop running in a different thread, the stop the world pauses is used in free-threading builds to stop all other running threads and find the task for the specified loop.

This also makes it easier for external introspection to find the current task, and now it will be always correct.
Lib/asyncio/tasks.py
Modules/_asynciomodule.c