@@ -500,15 +500,15 @@ async def comms(): # Perform some communications task
500
500
try :
501
501
await do_communications() # Launches Cancellable tasks
502
502
except CommsError:
503
- await Cancellable.cancel_all()
503
+ await asyn. Cancellable.cancel_all()
504
504
# All sub-tasks are now known to be stopped. They can be re-started
505
505
# with known initial state on next pass.
506
506
```
507
507
508
508
A ` Cancellable ` task is declared with the ` @cancellable ` decorator:
509
509
510
510
``` python
511
- @cancellable
511
+ @asyn. cancellable
512
512
async def print_nums (num ):
513
513
while True :
514
514
print (num)
@@ -521,16 +521,16 @@ constructor as below. Note that the coro is passed not using function call
521
521
syntax. ` Cancellable ` tasks may be awaited or placed on the event loop:
522
522
523
523
``` python
524
- await Cancellable(print_nums, 5 ) # single arg to print_nums.
524
+ await asyn. Cancellable(print_nums, 5 ) # single arg to print_nums.
525
525
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.
527
527
```
528
528
529
529
The following will cancel any tasks still running, pausing until cancellation
530
530
is complete:
531
531
532
532
``` python
533
- await Cancellable.cancel_all()
533
+ await asyn. Cancellable.cancel_all()
534
534
```
535
535
536
536
Constructor mandatory args:
@@ -565,6 +565,29 @@ Public bound method:
565
565
The ` asyn.StopTask ` exception is an alias for ` usayncio.CancelledError ` . In my
566
566
view the name is more descriptive of its function.
567
567
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
+
568
591
### 4.2.1 Groups
569
592
570
593
` Cancellable ` tasks may be assigned to groups, identified by a user supplied
@@ -577,7 +600,6 @@ with `cancel_all` cancelling all `Cancellable` tasks.
577
600
578
601
A task created with the ` cancellable ` decorator can intercept the ` StopTask `
579
602
exception to perform custom cleanup operations. This may be done as below:
580
-
581
603
``` python
582
604
@asyn.cancellable
583
605
async def foo ():
@@ -588,10 +610,8 @@ async def foo():
588
610
# perform custom cleanup
589
611
return # Respond by quitting
590
612
```
591
-
592
613
The following example returns ` True ` if it ends normally or ` False ` if
593
614
cancelled.
594
-
595
615
``` python
596
616
@asyn.cancellable
597
617
async def bar ():
@@ -602,6 +622,31 @@ async def bar():
602
622
else :
603
623
return True
604
624
```
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
+ ```
605
650
606
651
###### [ Contents] ( ./PRIMITIVES.md#contents )
607
652
0 commit comments