Celery从同一个功能创建多个任务

时间:2022-11-18 19:19:15

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.

但我在日志中看到update_paid和update_free都没有注册为任务。由于某种原因,_update被注册为任务。

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.

我不知道为什么celery / django-celery这样做,对我来说似乎很模糊。有谁知道如何解决这个问题?谢谢。

1 个解决方案

#1


1  

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:

Celery的任务修饰器在注册任务时使用修饰函数的名称,并且在定义函数时将此名称设置为“_update”:

>>> 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)

#1


1  

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:

Celery的任务修饰器在注册任务时使用修饰函数的名称,并且在定义函数时将此名称设置为“_update”:

>>> 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)