@@ -99,6 +99,10 @@ def maybe_unroll_group(g):
99
99
return g .tasks [0 ] if size == 1 else g
100
100
101
101
102
+ def task_name_from (task ):
103
+ return getattr (task , 'name' , task )
104
+
105
+
102
106
class Signature (dict ):
103
107
"""Class that wraps the arguments and execution options
104
108
for a single task invocation.
@@ -230,7 +234,7 @@ def set(self, immutable=None, **options):
230
234
def set_immutable (self , immutable ):
231
235
self .immutable = immutable
232
236
233
- def apply_async (self , args = (), kwargs = {}, ** options ):
237
+ def apply_async (self , args = (), kwargs = {}, route_name = None , ** options ):
234
238
try :
235
239
_apply = self ._apply_async
236
240
except IndexError : # no tasks for chain, etc to find type
@@ -240,7 +244,17 @@ def apply_async(self, args=(), kwargs={}, **options):
240
244
args , kwargs , options = self ._merge (args , kwargs , options )
241
245
else :
242
246
args , kwargs , options = self .args , self .kwargs , self .options
243
- return _apply (args , kwargs , ** options )
247
+ route_name = route_name or self .route_name_for (args , kwargs , options )
248
+ return _apply (args , kwargs , route_name = route_name , ** options )
249
+
250
+ def route_name_for (self , args , kwargs , options ):
251
+ """Can be used to override the name used for routing the task
252
+ to a queue.
253
+
254
+ If this returns :const:`None` the name of the task will be used.
255
+
256
+ """
257
+ pass
244
258
245
259
def append_to_list_option (self , key , value ):
246
260
items = self .options .setdefault (key , [])
@@ -309,6 +323,11 @@ def election(self):
309
323
def __repr__ (self ):
310
324
return self .reprcall ()
311
325
326
+ @property
327
+ def name (self ):
328
+ # for duck typing compatibility with Task.name
329
+ return self .task
330
+
312
331
@cached_property
313
332
def type (self ):
314
333
return self ._type or self .app .tasks [self ['task' ]]
@@ -485,11 +504,15 @@ def __init__(self, task, it, **options):
485
504
{'task' : task , 'it' : regen (it )}, immutable = True , ** options
486
505
)
487
506
507
+ def route_name_for (self , args , kwargs , options ):
508
+ return task_name_from (self .kwargs .get ('task' ))
509
+
488
510
def apply_async (self , args = (), kwargs = {}, ** opts ):
489
511
# need to evaluate generators
490
512
task , it = self ._unpack_args (self .kwargs )
491
513
return self .type .apply_async (
492
- (), {'task' : task , 'it' : list (it )}, ** opts
514
+ (), {'task' : task , 'it' : list (it )},
515
+ route_name = self .route_name_for (args , kwargs , opts ), ** opts
493
516
)
494
517
495
518
@classmethod
@@ -532,11 +555,17 @@ def __init__(self, task, it, n, **options):
532
555
def from_dict (self , d , app = None ):
533
556
return chunks (* self ._unpack_args (d ['kwargs' ]), app = app , ** d ['options' ])
534
557
558
+ def route_name_for (self , args , kwargs , options ):
559
+ return task_name_from (self .kwargs .get ('task' ))
560
+
535
561
def apply_async (self , args = (), kwargs = {}, ** opts ):
536
- return self .group ().apply_async (args , kwargs , ** opts )
562
+ return self .group ().apply_async (
563
+ args , kwargs ,
564
+ route_name = self .route_name_for (args , kwargs , opts ), ** opts
565
+ )
537
566
538
567
def __call__ (self , ** options ):
539
- return self .group () (** options )
568
+ return self .apply_async (** options )
540
569
541
570
def group (self ):
542
571
# need to evaluate generators
0 commit comments