Skip to content

Commit 9849e3e

Browse files
committed
PRIMITIVES.md add cancellation examples.
1 parent 5f760c9 commit 9849e3e

File tree

1 file changed

+53
-8
lines changed

1 file changed

+53
-8
lines changed

PRIMITIVES.md

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,15 +500,15 @@ async def comms(): # Perform some communications task
500500
try:
501501
await do_communications() # Launches Cancellable tasks
502502
except CommsError:
503-
await Cancellable.cancel_all()
503+
await asyn.Cancellable.cancel_all()
504504
# All sub-tasks are now known to be stopped. They can be re-started
505505
# with known initial state on next pass.
506506
```
507507

508508
A `Cancellable` task is declared with the `@cancellable` decorator:
509509

510510
```python
511-
@cancellable
511+
@asyn.cancellable
512512
async def print_nums(num):
513513
while True:
514514
print(num)
@@ -521,16 +521,16 @@ constructor as below. Note that the coro is passed not using function call
521521
syntax. `Cancellable` tasks may be awaited or placed on the event loop:
522522

523523
```python
524-
await Cancellable(print_nums, 5) # single arg to print_nums.
524+
await asyn.Cancellable(print_nums, 5) # single arg to print_nums.
525525
loop = asyncio.get_event_loop()
526-
loop.create_task(Cancellable(print_nums, 42)()) # Note () syntax.
526+
loop.create_task(asyn.Cancellable(print_nums, 42)()) # Note () syntax.
527527
```
528528

529529
The following will cancel any tasks still running, pausing until cancellation
530530
is complete:
531531

532532
```python
533-
await Cancellable.cancel_all()
533+
await asyn.Cancellable.cancel_all()
534534
```
535535

536536
Constructor mandatory args:
@@ -565,6 +565,29 @@ Public bound method:
565565
The `asyn.StopTask` exception is an alias for `usayncio.CancelledError`. In my
566566
view the name is more descriptive of its function.
567567

568+
A complete minimal, example:
569+
```python
570+
import uasyncio as asyncio
571+
import asyn
572+
573+
@asyn.cancellable
574+
async def print_nums(num):
575+
while True:
576+
print(num)
577+
num += 1
578+
await asyn.sleep(1) # asyn.sleep() allows fast response to exception
579+
580+
async def main(loop):
581+
loop.create_task(asyn.Cancellable(print_nums, 42)()) # Note () syntax
582+
await asyncio.sleep(5)
583+
await asyn.Cancellable.cancel_all()
584+
print('Task cancelled: delay 3 secs to prove it.')
585+
await asyncio.sleep(3)
586+
587+
loop = asyncio.get_event_loop()
588+
loop.run_until_complete(main(loop))
589+
```
590+
568591
### 4.2.1 Groups
569592

570593
`Cancellable` tasks may be assigned to groups, identified by a user supplied
@@ -577,7 +600,6 @@ with `cancel_all` cancelling all `Cancellable` tasks.
577600

578601
A task created with the `cancellable` decorator can intercept the `StopTask`
579602
exception to perform custom cleanup operations. This may be done as below:
580-
581603
```python
582604
@asyn.cancellable
583605
async def foo():
@@ -588,10 +610,8 @@ async def foo():
588610
# perform custom cleanup
589611
return # Respond by quitting
590612
```
591-
592613
The following example returns `True` if it ends normally or `False` if
593614
cancelled.
594-
595615
```python
596616
@asyn.cancellable
597617
async def bar():
@@ -602,6 +622,31 @@ async def bar():
602622
else:
603623
return True
604624
```
625+
A complete minimal example:
626+
```python
627+
import uasyncio as asyncio
628+
import asyn
629+
630+
@asyn.cancellable
631+
async def print_nums(num):
632+
try:
633+
while True:
634+
print(num)
635+
num += 1
636+
await asyn.sleep(1) # asyn.sleep() allows fast response to exception
637+
except asyn.StopTask:
638+
print('print_nums was cancelled')
639+
640+
async def main(loop):
641+
loop.create_task(asyn.Cancellable(print_nums, 42)()) # Note () syntax
642+
await asyncio.sleep(5)
643+
await asyn.Cancellable.cancel_all()
644+
print('Task cancelled: delay 3 secs to prove it.')
645+
await asyncio.sleep(3)
646+
647+
loop = asyncio.get_event_loop()
648+
loop.run_until_complete(main(loop))
649+
```
605650

606651
###### [Contents](./PRIMITIVES.md#contents)
607652

0 commit comments

Comments
 (0)