Skip to content

Commit a3a0685

Browse files
committed
PRIMITIVES.md Improve cancellation description.
1 parent 6b8d737 commit a3a0685

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

PRIMITIVES.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -316,21 +316,21 @@ Note to users of releases prior to 31st Dec 2017: this API has changed. It
316316
should now be stable.
317317

318318
In `uasyncio` task cancellation is achieved by throwing an exception to the
319-
coro to be cancelled. Cancellation occurs when it is next scheduled. If a coro
320-
issues `await uasyncio.sleep(secs)` or `uasyncio.sleep_ms(ms)` scheduling will
321-
not occur until the time has elapsed. This introduces latency into cancellation
322-
which matters in certain use-cases.
319+
coro to be cancelled in a special way: cancellation is deferred until the coro
320+
is next scheduled. This mechanism works with nested coros. However there is a
321+
limitation. If a coro issues `await uasyncio.sleep(secs)` or
322+
`uasyncio.sleep_ms(ms)` scheduling will not occur until the time has elapsed.
323+
This introduces latency into cancellation which matters in some use-cases.
323324

324325
Cancellation is supported by two classes, `Cancellable` and `NamedTask`. The
325326
`Cancellable` class allows the creation of named groups of anonymous tasks
326327
which may be cancelled as a group. Crucially this awaits actual completion of
327328
cancellation of all tasks in the group.
328329

329330
The `NamedTask` class enables a task to be associated with a user supplied
330-
name, enabling it to be cancelled and its status checked. This offers a higher
331-
degree of control than afforded by the `Cancellable` class, however
332-
cancellation does not await confirmation of completion. This may be achieved by
333-
means of the `Barrier` class. This is detailed below.
331+
name, enabling it to be cancelled and its status checked. Cancellation does not
332+
await confirmation of completion. This may be achieved by means of a `Barrier`
333+
instance although the normal approach is to use a `Cancellable` task.
334334

335335
For cases where cancellation latency is of concern `asyn.py` offers a `sleep`
336336
function which can reduce this.
@@ -341,19 +341,19 @@ Pause for a period as per `uasyncio.sleep` but with reduced exception handling
341341
latency.
342342

343343
The asynchronous `sleep` function takes two args:
344-
* `t` Time in seconds. May be integer or float.
345-
* `granularity` Integer >= 0, units ms. Default 100ms. Defines the maximum
346-
latency. Small values reduce latency at cost of increased scheduler workload.
344+
* `t` Mandatory. Time in seconds. May be integer or float.
345+
* `granularity` Optional. Integer >= 0, units ms. Default 100ms. Defines the
346+
maximum latency. Small values reduce latency at cost of increased scheduler
347+
workload.
347348

348349
This repeatedly issues `uasyncio.sleep_ms(t)` where t <= `granularity`.
349350

350351
## 4.2 Class Cancellable
351352

352-
This class is aimed at a common use case where a "teardown" task is required to
353-
cancel one or more other tasks, pausing until all have actually terminated.
354-
`Cancellable` instances are anonymous coros which are members of a named group.
355-
They are capable of being cancelled as a group. A typical use-case might take
356-
this form:
353+
This class provides for cancellation of one or more tasks where it is necesary
354+
to await confirmation that cancellation is complete. `Cancellable` instances
355+
are anonymous coros which are members of a named group. They are capable of
356+
being cancelled as a group. A typical use-case might take this form:
357357

358358
```python
359359
async def comms(): # Perform some communications task
@@ -546,14 +546,19 @@ the `StopTask` exception. Consequently calling `is_running()` on a recently
546546
cancelled task may return `False` even though `uasyncio` will run the task for
547547
one final time.
548548

549+
Confirmation of cancellation may be achieved by means of a `Barrier` object,
550+
however practical use-cases for this are few - if confirmation is required the
551+
normal approach is to use `Cancellable` tasks, if necessary in groups having a
552+
single member. However the approach is described below.
553+
549554
If a `Barrier` instance is passed to the `NamedTask` constructor, a task
550555
performing cancellation can pause until a set of cancelled tasks have
551556
terminated. The `Barrier` is constructed with the number of dependent tasks
552557
plus one (the task which is to wait on it). It is passed to the constructor of
553558
each dependent task and the cancelling task waits on it after cancelling all
554559
dependent tasks. Note that the tasks being cancelled terminate immediately.
555560

556-
See examples in `asyntest.py` e.g. `cancel_test2()`.
561+
See examples in `cantest.py` e.g. `cancel_test2()`.
557562

558563
### 4.3.2 Custom cleanup
559564

0 commit comments

Comments
 (0)