芹菜确定某些任务的具体时间限制

时间:2018-03-19 12:33:28

标签: python celery

我的任务写得像这样: 我的默认任务时间限制是30分钟。我想将此特定任务的时间限制增加到1小时。

@async_runner.app.task(name='task_name')
def async_task():
    async_runner.send_task(
        task_fn=task_processing,
        queue='queue_name',
        options=async_runner.DEFAULT_RETRY_POLICY
    )

除了默认时间限制外,如何为这一项任务设置不同的时间限制?

我已经查看了this,但我的问题是针对烧瓶以及如何在烧瓶中配置芹菜。 感谢。

1 个答案:

答案 0 :(得分:1)

根据芹菜官方documentation

  

“时间限制(-time-limit)是任务的最大秒数   可以在执行它的进程终止之前运行并替换为   一个新的过程。您还可以启用软时间限制   (-soft-time-limit),这会引发任务可以捕获的异常   在艰难的时间限制之前清理它“

因此,例如,如果您希望添加一个软时间限制并在达到限制时捕获异常,您可以执行以下操作:

from celery.exceptions import SoftTimeLimitExceeded

@async_runner.app.task(name='task_name', soft_time_limit=600)
def async_task():
    try:
        async_runner.send_task(
            task_fn=task_processing,
            queue='queue_name',
            options=async_runner.DEFAULT_RETRY_POLICY
        )
    except SoftTimeLimitExceeded as e:
        DO SOMETHING 
相关问题