You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: InternalDocs/asyncio.md
+5-7Lines changed: 5 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,9 +10,9 @@ This document describes the working and implementation details of C implementati
10
10
11
11
## Pre-Python 3.14 implementation
12
12
13
-
Until Python 3.13, the C implementation of `asyncio` used a [`WeakSet`](https://docs.python.org/3/library/weakref.html#weakref.WeakSet) to store all the tasks created by the event loop[^1]. `WeakSet` was used so that the event loop
13
+
Until Python 3.13, the C implementation of `asyncio` used a [`WeakSet`](https://docs.python.org/3/library/weakref.html#weakref.WeakSet) to store all the tasks created by the event loop. `WeakSet` was used so that the event loop
14
14
doesn't hold strong references to the tasks, allowing them to be garbage collected when they are no longer needed.
15
-
The current task of the event loop was stored in dict mapping the event loop to the current task[^2].
15
+
The current task of the event loop was stored in dict mapping the event loop to the current task.
16
16
17
17
```c
18
18
/* Dictionary containing tasks that are currently active in
@@ -27,7 +27,7 @@ This implementation had a few drawbacks:
27
27
1.**Performance**: Using a `WeakSet` for storing tasks is inefficient as it requires maintaining a full set of weak references to tasks along with corresponding weakref callback to cleanup the tasks when they are garbage collected.
28
28
This increases the work done by the garbage collector and in applications with a large number of tasks, this becomes a bottle neck, with increased memory usage and lower performance. Looking up the current task was slow as it required a dictionary lookup on the `current_tasks` dict.
29
29
30
-
2.**Thread safety**: Until Python 3.14, concurrent iterations over `WeakSet` was not thread safe[^3]. This meant calling APIs like `asyncio.all_tasks()` could lead to inconsistent results or even `RuntimeError` if used in multiple threads[^4].
30
+
2.**Thread safety**: Until Python 3.14, concurrent iterations over `WeakSet` was not thread safe[^1]. This meant calling APIs like `asyncio.all_tasks()` could lead to inconsistent results or even `RuntimeError` if used in multiple threads[^2].
31
31
32
32
3.**Poor scaling in free-threading**: Using global `WeakSet` for storing all tasks across all threads lead to contention when adding and removing tasks from the set which is a frequent operation. As such it performed poorly in free-threading and did not scale well with the number of threads. Similarly accessing the current task in multiple threads did not scale due to contention on the global `current_tasks` dictionary.
33
33
@@ -117,7 +117,5 @@ When a task is entered or left, the current task is updated in the thread state
0 commit comments