dragoon
dragoon

Reputation: 5744

Celery create multiple tasks from the same function

I want to create multiple celery tasks from the same function, and they will differ in the parameters that I pass to the task decorator. Let's say I want to set different timeouts for paid and free account in my system.

I was expecting that applying the task decorator in the following way will do the trick:

def _update(x, y):
    ...    

update_free = task(soft_time_limit=300, time_limit=305)(_update)

update_paid = task(time_limit=1800)(_update)

But I see in the log that neither update_paid nor update_free are registered as tasks. Instead for some reason _update is registered as a task.

I don't why celery/django-celery does this, seem to be quite obscure to me. Does anyone have any idea how to fix this? Thanks.

Upvotes: 2

Views: 1365

Answers (1)

Nicolas Cortot
Nicolas Cortot

Reputation: 6701

Celery's task decorators uses the decorated function's name when registering the task, and this name is set to "_update" when the function is defined:

>>> def _update(x, y):
...     pass
... 
>>> _update.__name__
  > '_update'
>>> update2 = _update
>>> update2.__name__
  > '_update'

You can specify the name of the task in the decorator though:

update_free = task(name='update_free', soft_time_limit=300, time_limit=305)(_update)
update_paid = task(name='update_paid', time_limit=1800)(_update)

Upvotes: 2

Related Questions