@@ -56,7 +56,7 @@ def printexp(exp, runtime=0):
56
56
# cancel_test1()
57
57
58
58
@asyn .cancellable
59
- async def foo (task_id , num ):
59
+ async def foo (num ):
60
60
try :
61
61
await asyncio .sleep (4 )
62
62
except asyn .StopTask :
@@ -109,7 +109,7 @@ async def forever(n):
109
109
110
110
# Intercepting the StopTask exception.
111
111
@asyn .cancellable
112
- async def rats (task_id , n ):
112
+ async def rats (n ):
113
113
try :
114
114
await forever (n )
115
115
except asyn .StopTask :
@@ -147,7 +147,7 @@ def test2():
147
147
148
148
# Intercepting the StopTask exception.
149
149
@asyn .cancellable
150
- async def cant3 (task_id ):
150
+ async def cant3 ():
151
151
try :
152
152
await asyncio .sleep (1 )
153
153
print ('Task cant3 has ended.' )
@@ -187,51 +187,48 @@ def test3():
187
187
# task_id automatically
188
188
189
189
@asyn .cancellable
190
- async def cant40 (task_id ):
191
- task_no = task_id ()
190
+ async def cant40 (num ):
192
191
while True :
193
192
try :
194
193
await asyn .sleep (1 )
195
- print ('Task cant40 no. {} running.' .format (task_no ))
194
+ print ('Task cant40 no. {} running.' .format (num ))
196
195
except asyn .StopTask :
197
- print ('Task cant40 no. {} was cancelled' .format (task_no ))
196
+ print ('Task cant40 no. {} was cancelled' .format (num ))
198
197
return
199
198
200
199
@asyn .cancellable
201
- async def cant41 (task_id , arg = 0 ):
202
- task_no = task_id ()
200
+ async def cant41 (num , arg = 0 ):
203
201
try :
204
202
await asyn .sleep (1 )
205
- print ('Task cant41 no. {} running, arg {}.' .format (task_no , arg ))
203
+ print ('Task cant41 no. {} running, arg {}.' .format (num , arg ))
206
204
except asyn .StopTask :
207
- print ('Task cant41 no. {} was cancelled.' .format (task_no ))
205
+ print ('Task cant41 no. {} was cancelled.' .format (num ))
208
206
return
209
207
else :
210
- print ('Task cant41 no. {} ended.' .format (task_no ))
208
+ print ('Task cant41 no. {} ended.' .format (num ))
211
209
212
- async def cant42 (task_no ):
210
+ async def cant42 (num ):
213
211
while True :
214
- print ('Task cant42 no. {} running' .format (task_no ))
212
+ print ('Task cant42 no. {} running' .format (num ))
215
213
await asyn .sleep (1.2 )
216
214
217
215
# Test await syntax and throwing exception to subtask
218
216
@asyn .cancellable
219
- async def chained (task_id , x , y , * , red , blue ):
217
+ async def chained (num , x , y , * , red , blue ):
220
218
print ('Args:' , x , y , red , blue ) # Test args and kwargs
221
- task_no = task_id ()
222
219
try :
223
- await cant42 (task_no )
220
+ await cant42 (num )
224
221
except asyn .StopTask :
225
- print ('Task chained no. {} was cancelled' .format (task_no ))
222
+ print ('Task chained no. {} was cancelled' .format (num ))
226
223
227
224
async def run_cancel_test4 ():
228
- await asyn .Cancellable (cant41 , 5 )
225
+ await asyn .Cancellable (cant41 , 0 )
229
226
loop = asyncio .get_event_loop ()
230
- loop .create_task (asyn .Cancellable (cant40 )()) # 3 instances in default group 0
231
- loop .create_task (asyn .Cancellable (cant40 )())
232
- loop .create_task (asyn .Cancellable (cant40 )())
233
- loop .create_task (asyn .Cancellable (chained , 1 , 2 , red = 3 , blue = 4 , group = 1 )())
234
- loop .create_task (asyn .Cancellable (cant41 )()) # Runs to completion
227
+ loop .create_task (asyn .Cancellable (cant40 , 1 )()) # 3 instances in default group 0
228
+ loop .create_task (asyn .Cancellable (cant40 , 2 )())
229
+ loop .create_task (asyn .Cancellable (cant40 , 3 )())
230
+ loop .create_task (asyn .Cancellable (chained , 4 , 1 , 2 , red = 3 , blue = 4 , group = 1 )())
231
+ loop .create_task (asyn .Cancellable (cant41 , 5 )()) # Runs to completion
235
232
print ('Running tasks' )
236
233
await asyncio .sleep (3 )
237
234
print ('About to cancel group 0 tasks' )
@@ -288,7 +285,7 @@ async def start(self, loop):
288
285
print ('Done' )
289
286
290
287
@asyn .cancellable
291
- async def foo (self , _ , arg ):
288
+ async def foo (self , arg ):
292
289
try :
293
290
while True :
294
291
await asyn .sleep (1 )
@@ -297,7 +294,7 @@ async def foo(self, _, arg):
297
294
print ('foo was cancelled' )
298
295
299
296
@asyn .cancellable
300
- async def bar (self , _ , arg , * , x = 1 , y = 2 ):
297
+ async def bar (self , arg , * , x = 1 , y = 2 ):
301
298
try :
302
299
while True :
303
300
await asyn .sleep (1 )
@@ -334,20 +331,19 @@ def test5():
334
331
335
332
# test 6: test NamedTask.is_running()
336
333
@asyn .cancellable
337
- async def cant60 (task_id , name ):
338
- task_no = task_id ()
339
- print ('Task cant60 no. {} name \" {}\" running.' .format (task_no , name ))
334
+ async def cant60 (name ):
335
+ print ('Task cant60 name \" {}\" running.' .format (name ))
340
336
try :
341
337
for _ in range (5 ):
342
338
await asyncio .sleep (2 ) # 2 secs latency.
343
339
except asyn .StopTask :
344
- print ('Task cant60 no. {} name \" {}\" was cancelled.' .format (task_no , name ))
340
+ print ('Task cant60 name \" {}\" was cancelled.' .format (name ))
345
341
return
346
342
else :
347
- print ('Task cant60 no. {} name \" {}\" ended.' .format (task_no , name ))
343
+ print ('Task cant60 name \" {}\" ended.' .format (name ))
348
344
349
345
@asyn .cancellable
350
- async def cant61 (task_id ):
346
+ async def cant61 ():
351
347
try :
352
348
while True :
353
349
for name in ('complete' , 'cancel me' ):
@@ -378,8 +374,8 @@ async def run_cancel_test6(loop):
378
374
379
375
380
376
def test6 ():
381
- printexp ('''Task cant60 no. 0 name "complete" running.
382
- Task cant60 no. 1 name "cancel me" running.
377
+ printexp ('''Task cant60 name "complete" running.
378
+ Task cant60 name "cancel me" running.
383
379
Task "complete" running: True
384
380
Task "cancel me" running: True
385
381
Task "complete" running: True
@@ -393,7 +389,7 @@ def test6():
393
389
Cancelling task "cancel me". 1.5 secs latency.
394
390
Task "complete" running: True
395
391
Task "cancel me" running: True
396
- Task cant60 no. 1 name "cancel me" was cancelled.
392
+ Task cant60 name "cancel me" was cancelled.
397
393
Task "complete" running: True
398
394
Task "cancel me" running: False
399
395
Task "complete" running: True
@@ -402,18 +398,18 @@ def test6():
402
398
Task "cancel me" running: False
403
399
Task "complete" running: True
404
400
Task "cancel me" running: False
405
- Task cant60 no. 0 name "complete" ended.
401
+ Task cant60 name "complete" ended.
406
402
Task "complete" running: False
407
403
Task "cancel me" running: False
408
404
Task "complete" running: False
409
405
Task "cancel me" running: False
410
- Task cant60 no. 3 name "cancel wait" running.
406
+ Task cant60 name "cancel wait" running.
411
407
Cancelling task "cancel wait". 1.5 secs latency.
412
408
Task "complete" running: False
413
409
Task "cancel me" running: False
414
410
Task "complete" running: False
415
411
Task "cancel me" running: False
416
- Task cant60 no. 3 name "cancel wait" was cancelled.
412
+ Task cant60 name "cancel wait" was cancelled.
417
413
Was cancelled in 1503 ms
418
414
Cancelling cant61
419
415
Task cant61 cancelled.
0 commit comments