25
25
26
26
import uasyncio as asyncio
27
27
import asyn
28
+ import utime as time
28
29
29
30
def print_tests ():
30
31
st = '''Available functions:
@@ -33,6 +34,7 @@ def print_tests():
33
34
test3() Cancellation of a NamedTask which has run to completion.
34
35
test4() Test of Cancellable class.
35
36
test5() Cancellable and NamedTask instances as bound methods.
37
+ test6() Test of NamedTask.is_running() and awaiting NamedTask cancellation.
36
38
Recommended to issue ctrl-D after running each test.
37
39
'''
38
40
print ('\x1b [32m' )
@@ -327,3 +329,96 @@ def test5():
327
329
cantest = CanTest ()
328
330
loop = asyncio .get_event_loop ()
329
331
loop .run_until_complete (cantest .start (loop ))
332
+
333
+ # test 6: test NamedTask.is_running()
334
+ @asyn .cancellable
335
+ async def cant60 (task_id , name ):
336
+ task_no = task_id ()
337
+ print ('Task cant60 no. {} name \" {}\" running.' .format (task_no , name ))
338
+ try :
339
+ for _ in range (5 ):
340
+ await asyncio .sleep (2 ) # 2 secs latency.
341
+ except asyn .StopTask :
342
+ print ('Task cant60 no. {} name \" {}\" was cancelled.' .format (task_no , name ))
343
+ return
344
+ else :
345
+ print ('Task cant60 no. {} name \" {}\" ended.' .format (task_no , name ))
346
+
347
+ @asyn .cancellable
348
+ async def cant61 (task_id ):
349
+ try :
350
+ while True :
351
+ for name in ('complete' , 'cancel me' ):
352
+ res = asyn .NamedTask .is_running (name )
353
+ print ('Task \" {}\" running: {}' .format (name , res ))
354
+ await asyncio .sleep (1 )
355
+ except asyn .StopTask :
356
+ print ('Task cant61 cancelled.' )
357
+
358
+ async def run_cancel_test6 (loop ):
359
+ for name in ('complete' , 'cancel me' ):
360
+ loop .create_task (asyn .NamedTask (name , cant60 , name )())
361
+ loop .create_task (asyn .Cancellable (cant61 )())
362
+ await asyncio .sleep (4.5 )
363
+ print ('Cancelling task \" {}\" . 1.5 secs latency.' .format (name ))
364
+ await asyn .NamedTask .cancel (name )
365
+ await asyncio .sleep (7 )
366
+ name = 'cancel wait'
367
+ loop .create_task (asyn .NamedTask (name , cant60 , name )())
368
+ await asyncio .sleep (0.5 )
369
+ print ('Cancelling task \" {}\" . 1.5 secs latency.' .format (name ))
370
+ t = time .ticks_ms ()
371
+ await asyn .NamedTask .cancel ('cancel wait' , nowait = False )
372
+ print ('Was cancelled in {} ms' .format (time .ticks_diff (time .ticks_ms (), t )))
373
+ print ('Cancelling cant61' )
374
+ await asyn .Cancellable .cancel_all ()
375
+ print ('Done' )
376
+
377
+
378
+ def test6 ():
379
+ printexp ('''Task cant60 no. 0 name "complete" running.
380
+ Task cant60 no. 1 name "cancel me" running.
381
+ Task "complete" running: True
382
+ Task "cancel me" running: True
383
+ Task "complete" running: True
384
+ Task "cancel me" running: True
385
+ Task "complete" running: True
386
+ Task "cancel me" running: True
387
+ Task "complete" running: True
388
+ Task "cancel me" running: True
389
+ Task "complete" running: True
390
+ Task "cancel me" running: True
391
+ Cancelling task "cancel me". 1.5 secs latency.
392
+ Task "complete" running: True
393
+ Task "cancel me" running: True
394
+ Task cant60 no. 1 name "cancel me" was cancelled.
395
+ Task "complete" running: True
396
+ Task "cancel me" running: False
397
+ Task "complete" running: True
398
+ Task "cancel me" running: False
399
+ Task "complete" running: True
400
+ Task "cancel me" running: False
401
+ Task "complete" running: True
402
+ Task "cancel me" running: False
403
+ Task cant60 no. 0 name "complete" ended.
404
+ Task "complete" running: False
405
+ Task "cancel me" running: False
406
+ Task "complete" running: False
407
+ Task "cancel me" running: False
408
+ Task cant60 no. 3 name "cancel wait" running.
409
+ Cancelling task "cancel wait". 1.5 secs latency.
410
+ Task "complete" running: False
411
+ Task "cancel me" running: False
412
+ Task "complete" running: False
413
+ Task "cancel me" running: False
414
+ Task cant60 no. 3 name "cancel wait" was cancelled.
415
+ Was cancelled in 1503 ms
416
+ Cancelling cant61
417
+ Task cant61 cancelled.
418
+ Done
419
+
420
+
421
+ [Duration of cancel wait may vary depending on platform 1500 <= range <= 1600ms]
422
+ ''' , 14 )
423
+ loop = asyncio .get_event_loop ()
424
+ loop .run_until_complete (run_cancel_test6 (loop ))
0 commit comments