@@ -316,21 +316,21 @@ Note to users of releases prior to 31st Dec 2017: this API has changed. It
316
316
should now be stable.
317
317
318
318
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.
323
324
324
325
Cancellation is supported by two classes, ` Cancellable ` and ` NamedTask ` . The
325
326
` Cancellable ` class allows the creation of named groups of anonymous tasks
326
327
which may be cancelled as a group. Crucially this awaits actual completion of
327
328
cancellation of all tasks in the group.
328
329
329
330
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.
334
334
335
335
For cases where cancellation latency is of concern ` asyn.py ` offers a ` sleep `
336
336
function which can reduce this.
@@ -341,19 +341,19 @@ Pause for a period as per `uasyncio.sleep` but with reduced exception handling
341
341
latency.
342
342
343
343
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.
347
348
348
349
This repeatedly issues ` uasyncio.sleep_ms(t) ` where t <= ` granularity ` .
349
350
350
351
## 4.2 Class Cancellable
351
352
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:
357
357
358
358
``` python
359
359
async def comms (): # Perform some communications task
@@ -546,14 +546,19 @@ the `StopTask` exception. Consequently calling `is_running()` on a recently
546
546
cancelled task may return ` False ` even though ` uasyncio ` will run the task for
547
547
one final time.
548
548
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
+
549
554
If a ` Barrier ` instance is passed to the ` NamedTask ` constructor, a task
550
555
performing cancellation can pause until a set of cancelled tasks have
551
556
terminated. The ` Barrier ` is constructed with the number of dependent tasks
552
557
plus one (the task which is to wait on it). It is passed to the constructor of
553
558
each dependent task and the cancelling task waits on it after cancelling all
554
559
dependent tasks. Note that the tasks being cancelled terminate immediately.
555
560
556
- See examples in ` asyntest .py` e.g. ` cancel_test2() ` .
561
+ See examples in ` cantest .py` e.g. ` cancel_test2() ` .
557
562
558
563
### 4.3.2 Custom cleanup
559
564
0 commit comments