简体   繁体   English

芹菜:为某些任务分配特定的时间限制

[英]Celery: Assigning specific time limit for certain Task

I have a Task written like this:我有一个这样写的任务:

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

My default task time limit is 30 mins.我的默认任务时间限制是 30 分钟。 I want to increase the time limit for this certain task to 1 hour.我想将此特定任务的时间限制增加到 1 小时。

How can I set a different time limit for this one task?如何为这一项任务设置不同的时间限制?

I have already looked at this but my question is specific to Flask and how Celery is configured in Flask.我已经看过这个,但我的问题是针对 Flask 的,以及 Celery 在 Flask 中的配置方式。 Thanks.谢谢。

As per the official Celery documentation ,根据官方 Celery 文档

"The time limit (–time-limit) is the maximum number of seconds a task may run before the process executing it is terminated and replaced by a new process. You can also enable a soft time limit (–soft-time-limit), this raises an exception the task can catch to clean up before the hard time limit kills it" “时间限制(–time-limit)是任务在执行它的进程被终止并被新进程替换之前可能运行的最大秒数。您还可以启用软时间限制(–soft-time-limit) ,这引发了一个异常,任务可以在硬时间限制杀死它之前捕捉到清理“

So, for example, if you wish to add a soft time limit and catch an exception should the limit be reached, you could do something like this:因此,例如,如果您希望添加软时间限制并在达到限制时捕获异常,您可以执行以下操作:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM