@@ -642,9 +642,13 @@ threaded through all core function calls as the "[current task]".
642
642
643
643
Tasks are parameterized by the caller with 3 callbacks of the following types:
644
644
``` python
645
+ class Cancelled (IntEnum ):
646
+ FALSE = 0
647
+ TRUE = 1
648
+
645
649
OnStart = Callable[[], list[any ]]
646
650
OnResolve = Callable[[Optional[list[any ]]], None ]
647
- OnBlock = Callable[[Awaitable], Awaitable[bool ]]
651
+ OnBlock = Callable[[Awaitable], Awaitable[Cancelled ]]
648
652
```
649
653
and with the following meanings:
650
654
* The ` OnStart ` callback is called by the task when the task is ready to start
@@ -660,8 +664,8 @@ and with the following meanings:
660
664
on a Python [ awaitable] . ` OnBlock ` allows a transitive (async) supertask to
661
665
take control flow while its subtask is blocked. During a call to ` OnBlock ` ,
662
666
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
665
669
cancellation; in this case, the given awaitable may not be resolved, and the
666
670
cancelled task should call ` OnResolve ` ASAP (potentially passing ` None ` ).
667
671
@@ -711,11 +715,11 @@ called:
711
715
if not self .may_enter(self ) or self .inst.pending_tasks:
712
716
f = asyncio.Future()
713
717
self .inst.pending_tasks.append((self , f))
714
- if await self .on_block(f):
718
+ if await self .on_block(f) == Cancelled. TRUE :
715
719
[i] = [i for i,(t,_) in enumerate (self .inst.pending_tasks) if t == self ]
716
720
self .inst.pending_tasks.pop(i)
717
721
self .on_resolve(None )
718
- return False
722
+ return Cancelled. FALSE
719
723
assert (self .may_enter(self ) and self .inst.starting_pending_task)
720
724
self .inst.starting_pending_task = False
721
725
if self .opts.sync:
@@ -830,7 +834,7 @@ Python [awaitable] using the `OnBlock` callback described above:
830
834
831
835
awaitable = asyncio.ensure_future(awaitable)
832
836
if awaitable.done() and not DETERMINISTIC_PROFILE and random.randint(0 ,1 ):
833
- cancelled = False
837
+ cancelled = Cancelled. FALSE
834
838
else :
835
839
cancelled = await self .on_block(awaitable)
836
840
if cancelled and not cancellable:
@@ -878,11 +882,11 @@ the calls in the stack actually block on external I/O.
878
882
``` python
879
883
async def call_sync (self , callee , on_start , on_return ):
880
884
async def sync_on_block (awaitable ):
881
- if await self .on_block(awaitable):
885
+ if await self .on_block(awaitable) == Cancelled. TRUE :
882
886
assert (self .state == Task.State.INITIAL )
883
887
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
886
890
887
891
assert (not self .inst.calling_sync_import)
888
892
self .inst.calling_sync_import = True
@@ -1132,12 +1136,12 @@ cancellation:
1132
1136
await asyncio.wait([awaitable, self .request_cancel_begin],
1133
1137
return_when = asyncio.FIRST_COMPLETED )
1134
1138
if self .request_cancel_begin.done():
1135
- return True
1139
+ return Cancelled. TRUE
1136
1140
else :
1137
1141
await awaitable
1138
1142
assert (awaitable.done())
1139
1143
await scheduler.acquire()
1140
- return False
1144
+ return Cancelled. FALSE
1141
1145
1142
1146
def relinquish_control ():
1143
1147
if not ret.done():
0 commit comments