Skip to content

Commit 45cf5b0

Browse files
pythongh-109955 : Update state transition comments for asyncio.Task (python#109910)
Co-authored-by: Adam Turner <[email protected]>
1 parent 32466c9 commit 45cf5b0

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

Lib/asyncio/tasks.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,25 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
7373
"""A coroutine wrapped in a Future."""
7474

7575
# An important invariant maintained while a Task not done:
76+
# _fut_waiter is either None or a Future. The Future
77+
# can be either done() or not done().
78+
# The task can be in any of 3 states:
7679
#
77-
# - Either _fut_waiter is None, and _step() is scheduled;
78-
# - or _fut_waiter is some Future, and _step() is *not* scheduled.
80+
# - 1: _fut_waiter is not None and not _fut_waiter.done():
81+
# __step() is *not* scheduled and the Task is waiting for _fut_waiter.
82+
# - 2: (_fut_waiter is None or _fut_waiter.done()) and __step() is scheduled:
83+
# the Task is waiting for __step() to be executed.
84+
# - 3: _fut_waiter is None and __step() is *not* scheduled:
85+
# the Task is currently executing (in __step()).
7986
#
80-
# The only transition from the latter to the former is through
81-
# _wakeup(). When _fut_waiter is not None, one of its callbacks
82-
# must be _wakeup().
83-
84-
# If False, don't log a message if the task is destroyed whereas its
87+
# * In state 1, one of the callbacks of __fut_waiter must be __wakeup().
88+
# * The transition from 1 to 2 happens when _fut_waiter becomes done(),
89+
# as it schedules __wakeup() to be called (which calls __step() so
90+
# we way that __step() is scheduled).
91+
# * It transitions from 2 to 3 when __step() is executed, and it clears
92+
# _fut_waiter to None.
93+
94+
# If False, don't log a message if the task is destroyed while its
8595
# status is still pending
8696
_log_destroy_pending = True
8797

0 commit comments

Comments
 (0)