@@ -642,9 +642,13 @@ threaded through all core function calls as the "[current task]".
642642
643643Tasks are parameterized by the caller with 3 callbacks of the following types:
644644``` python
645+ class Cancelled (IntEnum ):
646+ FALSE = 0
647+ TRUE = 1
648+
645649OnStart = Callable[[], list[any ]]
646650OnResolve = Callable[[Optional[list[any ]]], None ]
647- OnBlock = Callable[[Awaitable], Awaitable[bool ]]
651+ OnBlock = Callable[[Awaitable], Awaitable[Cancelled ]]
648652```
649653and with the following meanings:
650654* The ` OnStart ` callback is called by the task when the task is ready to start
@@ -660,8 +664,8 @@ and with the following meanings:
660664 on a Python [ awaitable] . ` OnBlock ` allows a transitive (async) supertask to
661665 take control flow while its subtask is blocked. During a call to ` OnBlock ` ,
662666 any other ` asyncio.Task ` s can be scheduled or new ` asyncio.Task ` s can be
663- started in response to new export calls. ` OnBlock ` may return ` True ` at most
664- once before a task is resolved to signal that the caller is requesting
667+ started in response to new export calls. ` OnBlock ` may return ` Cancelled.TRUE `
668+ at most once before a task is resolved to signal that the caller is requesting
665669 cancellation; in this case, the given awaitable may not be resolved, and the
666670 cancelled task should call ` OnResolve ` ASAP (potentially passing ` None ` ).
667671
@@ -711,11 +715,11 @@ called:
711715 if not self .may_enter(self ) or self .inst.pending_tasks:
712716 f = asyncio.Future()
713717 self .inst.pending_tasks.append((self , f))
714- if await self .on_block(f):
718+ if await self .on_block(f) == Cancelled. TRUE :
715719 [i] = [i for i,(t,_) in enumerate (self .inst.pending_tasks) if t == self ]
716720 self .inst.pending_tasks.pop(i)
717721 self .on_resolve(None )
718- return False
722+ return Cancelled. FALSE
719723 assert (self .may_enter(self ) and self .inst.starting_pending_task)
720724 self .inst.starting_pending_task = False
721725 if self .opts.sync:
@@ -830,7 +834,7 @@ Python [awaitable] using the `OnBlock` callback described above:
830834
831835 awaitable = asyncio.ensure_future(awaitable)
832836 if awaitable.done() and not DETERMINISTIC_PROFILE and random.randint(0 ,1 ):
833- cancelled = False
837+ cancelled = Cancelled. FALSE
834838 else :
835839 cancelled = await self .on_block(awaitable)
836840 if cancelled and not cancellable:
@@ -878,11 +882,11 @@ the calls in the stack actually block on external I/O.
878882``` python
879883 async def call_sync (self , callee , on_start , on_return ):
880884 async def sync_on_block (awaitable ):
881- if await self .on_block(awaitable):
885+ if await self .on_block(awaitable) == Cancelled. TRUE :
882886 assert (self .state == Task.State.INITIAL )
883887 self .state = Task.State.PENDING_CANCEL
884- assert (not await self .on_block(awaitable))
885- return False
888+ assert (await self .on_block(awaitable) == Cancelled. FALSE )
889+ return Cancelled. FALSE
886890
887891 assert (not self .inst.calling_sync_import)
888892 self .inst.calling_sync_import = True
@@ -1132,12 +1136,12 @@ cancellation:
11321136 await asyncio.wait([awaitable, self .request_cancel_begin],
11331137 return_when = asyncio.FIRST_COMPLETED )
11341138 if self .request_cancel_begin.done():
1135- return True
1139+ return Cancelled. TRUE
11361140 else :
11371141 await awaitable
11381142 assert (awaitable.done())
11391143 await scheduler.acquire()
1140- return False
1144+ return Cancelled. FALSE
11411145
11421146 def relinquish_control ():
11431147 if not ret.done():
0 commit comments