简体   繁体   中英

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. I want to increase the time limit for this certain task to 1 hour.

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. Thanks.

As per the official Celery documentation ,

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

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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