Skip to content

Commit f9a9830

Browse files
committed
Cancellable and Named tasks can accept kwargs.
1 parent acf76b7 commit f9a9830

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

PRIMITIVES.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,10 @@ Constructor mandatory args:
395395
Constructor optional positional args:
396396
* Any further positional args are passed to the coro.
397397

398-
Constructor optional keyword arg:
398+
Constructor optional keyword args:
399399
* `group` Any Python object, typically integer or string. Default 0. See
400400
Groups below.
401+
* Further keyword args are passed to the coro.
401402

402403
Public class method:
403404
* `cancel_all` Asynchronous.
@@ -508,8 +509,9 @@ Mandatory args:
508509
Optional positional args:
509510
* Any further positional args are passed to the coro.
510511

511-
Optional keyword only arg:
512+
Optional keyword only args:
512513
* `barrier` A `Barrier` instance may be passed. See below.
514+
* Further keyword args are passed to the coro.
513515

514516
Public class methods:
515517
* `cancel` Asynchronous.

asyn.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ def _stopped(cls, task_no):
306306
barrier.trigger()
307307
del cls.tasks[task_no]
308308

309-
def __init__(self, gf, *args, group=0):
310-
task = gf(TaskId(Cancellable.task_no), *args)
309+
def __init__(self, gf, *args, group=0, **kwargs):
310+
task = gf(TaskId(Cancellable.task_no), *args, **kwargs)
311311
if task in self.tasks:
312312
raise ValueError('Task already exists.')
313313
self.tasks[Cancellable.task_no] = [task, group, None]
@@ -327,8 +327,8 @@ def __await__(self):
327327
# @cancellable decorator
328328

329329
def cancellable(f):
330-
def new_gen(*args):
331-
g = f(*args)
330+
def new_gen(*args, **kwargs):
331+
g = f(*args, **kwargs)
332332
# Task ID is args[1] if a bound method (else args[0])
333333
idx = 0 if isinstance(args[0], TaskId) else 1
334334
task_id = args[idx]
@@ -367,10 +367,10 @@ def _stopped(cls, task_id): # On completion remove it
367367
del cls.instances[name]
368368
Cancellable._stopped(task_id())
369369

370-
def __init__(self, name, gf, *args, barrier=None):
370+
def __init__(self, name, gf, *args, barrier=None, **kwargs):
371371
if name in self.instances:
372372
raise ValueError('Task name "{}" already exists.'.format(name))
373-
super().__init__(gf, *args, group=name)
373+
super().__init__(gf, *args, group=name, **kwargs)
374374
self.barrier = barrier
375375
self.instances[name] = self
376376

cantest.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ async def cant42(task_no):
216216

217217
# Test await syntax and throwing exception to subtask
218218
@asyn.cancellable
219-
async def chained(task_id):
219+
async def chained(task_id, x, y, *, red, blue):
220+
print('Args:', x, y, red, blue) # Test args and kwargs
220221
task_no = task_id()
221222
try:
222223
await cant42(task_no)
@@ -229,7 +230,7 @@ async def run_cancel_test4():
229230
loop.create_task(asyn.Cancellable(cant40)()) # 3 instances in default group 0
230231
loop.create_task(asyn.Cancellable(cant40)())
231232
loop.create_task(asyn.Cancellable(cant40)())
232-
loop.create_task(asyn.Cancellable(chained, group=1)())
233+
loop.create_task(asyn.Cancellable(chained, 1, 2, red=3, blue=4, group=1)())
233234
loop.create_task(asyn.Cancellable(cant41)()) # Runs to completion
234235
print('Running tasks')
235236
await asyncio.sleep(3)
@@ -246,6 +247,7 @@ def test4():
246247
printexp('''Task cant41 no. 0 running, arg 5.
247248
Task cant41 no. 0 ended.
248249
Running tasks
250+
Args: 1 2 3 4
249251
Task cant42 no. 4 running
250252
Task cant40 no. 1 running.
251253
Task cant40 no. 2 running.
@@ -278,7 +280,7 @@ async def start(self, loop):
278280
loop.create_task(asyn.Cancellable(self.foo, 1)()) # 3 instances in default group 0
279281
loop.create_task(asyn.Cancellable(self.foo, 2)())
280282
loop.create_task(asyn.Cancellable(self.foo, 3)())
281-
loop.create_task(asyn.NamedTask('my bar', self.bar, 4)())
283+
loop.create_task(asyn.NamedTask('my bar', self.bar, 4, y=42)())
282284
await asyncio.sleep(4.5)
283285
await asyn.NamedTask.cancel('my bar')
284286
await asyn.Cancellable.cancel_all()
@@ -295,31 +297,31 @@ async def foo(self, _, arg):
295297
print('foo was cancelled')
296298

297299
@asyn.cancellable
298-
async def bar(self, _, arg):
300+
async def bar(self, _, arg, *, x=1, y=2):
299301
try:
300302
while True:
301303
await asyn.sleep(1)
302-
print('bar running, arg', arg)
304+
print('bar running, arg', arg, x, y)
303305
except asyn.StopTask:
304306
print('bar was cancelled')
305307

306308
def test5():
307309
printexp('''foo running, arg 1
308310
foo running, arg 2
309311
foo running, arg 3
310-
bar running, arg 4
312+
bar running, arg 4 1 42
311313
foo running, arg 1
312314
foo running, arg 2
313315
foo running, arg 3
314-
bar running, arg 4
316+
bar running, arg 4 1 42
315317
foo running, arg 1
316318
foo running, arg 2
317319
foo running, arg 3
318-
bar running, arg 4
320+
bar running, arg 4 1 42
319321
foo running, arg 1
320322
foo running, arg 2
321323
foo running, arg 3
322-
bar running, arg 4
324+
bar running, arg 4 1 42
323325
foo was cancelled
324326
foo was cancelled
325327
foo was cancelled

0 commit comments

Comments
 (0)