Skip to content

Conversation

@kumaraditya303
Copy link
Contributor

@kumaraditya303 kumaraditya303 commented Feb 9, 2025

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.

@kumaraditya303 kumaraditya303 enabled auto-merge (squash) February 19, 2025 16:12
@kumaraditya303 kumaraditya303 merged commit 660f126 into python:main Feb 19, 2025
41 checks passed
@kumaraditya303 kumaraditya303 deleted the per-thread-cur-task branch February 19, 2025 16:35
Copy link
Member

@1st1 1st1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

post merge: lgtm

@alanjds
Copy link

alanjds commented Nov 1, 2025

If I can ask: After this change, what is missing to make Asyncio have a job-stealing behavior like Golang and Tokio? Out of making an empty thread to move waiting coroutines from other threads to it's own?

Chaoses-Ib added a commit to Chaoses-Ib/nest-asyncio2 that referenced this pull request Nov 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants