【问题标题】:Celery create multiple tasks from the same functionCelery 从同一个函数创建多个任务
【发布时间】:2013-04-28 11:05:46
【问题描述】:

我想从同一个函数创建多个 celery 任务,它们在我传递给任务装饰器的参数上会有所不同。假设我想在我的系统中为付费和免费帐户设置不同的超时时间。

我期待通过以下方式应用任务装饰器可以解决问题:

def _update(x, y):
    ...    

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

update_paid = task(time_limit=1800)(_update)

但我在日志中看到update_paidupdate_free 都没有注册为任务。相反,由于某种原因,_update 被注册为一个任务。

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

【问题讨论】:

    标签: django celery django-celery


    【解决方案1】:

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

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

    您可以在装饰器中指定任务的名称:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 2017-03-27
      相关资源
      最近更新 更多