Skip to content

Commit 3d4e398

Browse files
committed
Tutorial: improve task cancellation section.
1 parent acdae03 commit 3d4e398

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

TUTORIAL.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,10 +614,24 @@ is next scheduled. This mechanism works with nested coros. However there is a
614614
limitation. If a coro issues `await uasyncio.sleep(secs)` or
615615
`uasyncio.sleep_ms(ms)` scheduling will not occur until the time has elapsed.
616616
This introduces latency into cancellation which matters in some use-cases.
617-
Other potential sources of latency take the form of slow code.
617+
Another source of latency is where a task is waiting on I/O. In many
618+
applications it is necessary for the task performing cancellation to pause
619+
until all cancelled coros have actually stopped.
618620

619-
`uasyncio` lacks a mechanism for verifying when cancellation has actually
620-
occurred. The `asyn` library provides verification via the following classes:
621+
If the task to be cancelled only pauses on zero delays and never waits on I/O,
622+
the round-robin nature of the scheduler avoids the need to verify cancellation:
623+
624+
```python
625+
asyncio.cancel(my_coro)
626+
await asyncio.sleep(0) # Ensure my_coro gets scheduled with the exception
627+
# my_coro will be cancelled now
628+
```
629+
This does require that all coros awaited by `my_coro` also meet the zero delay
630+
criterion.
631+
632+
That special case notwithstanding, `uasyncio` lacks a mechanism for verifying
633+
when cancellation has actually occurred. The `asyn` library provides
634+
verification via the following classes:
621635

622636
1. `Cancellable` This allows one or more tasks to be assigned to a group. A
623637
coro can cancel all tasks in the group, pausing until this has been achieved.

0 commit comments

Comments
 (0)