Skip to content

Commit 88fca20

Browse files
add free-threading note
1 parent 2a5a2b1 commit 88fca20

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

InternalDocs/asyncio.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ flowchart TD
9595
end
9696
9797
one --> two
98-
9998
```
10099

101100
`asyncio.all_tasks` now iterates over the per-thread task lists of all threads and the interpreter's task list to get all the tasks. In free-threading this is done by pausing all the threads using the `stop-the-world` pause to ensure that no tasks are being added or removed while iterating over the lists. This allows for a consistent view of all task lists across all threads and is thread safe.
@@ -113,8 +112,9 @@ typedef struct PyThreadState {
113112
} PyThreadState;
114113
```
115114

116-
When a task is entered or left, the current task is updated in the thread state using `enter_task` and `leave_task` functions. When `current_task(loop)` is called where `loop` is the current running event loop of the current thread, no locking is required as the current task is stored in the thread state and is returned directly. Otherwise, if the `loop` is not current running event loop, the `stop-the-world` pause is used to pause all threads in free-threading and then by iterating over all the thread states and checking if the `loop` matches with `tstate->asyncio_current_loop`, the current task is found and returned. If no matching thread state is found, `None` is returned.
115+
When a task is entered or left, the current task is updated in the thread state using `enter_task` and `leave_task` functions. When `current_task(loop)` is called where `loop` is the current running event loop of the current thread, no locking is required as the current task is stored in the thread state and is returned directly (general case). Otherwise, if the `loop` is not current running event loop, the `stop-the-world` pause is used to pause all threads in free-threading and then by iterating over all the thread states and checking if the `loop` matches with `tstate->asyncio_current_loop`, the current task is found and returned. If no matching thread state is found, `None` is returned.
117116

117+
In free-threading, it avoids contention on a global dictionary as threads can access the current task of thier running loop without any locking.
118118

119119

120120
[^1]: https://github.com/python/cpython/issues/123089

0 commit comments

Comments
 (0)