1

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.

Victor Le Pochat
  • 241
  • 4
  • 11
Kishan Mehta
  • 2,598
  • 5
  • 39
  • 61
  • Possible duplicate of [Setting Time Limit on specific task with celery](https://stackoverflow.com/questions/11672179/setting-time-limit-on-specific-task-with-celery) – Florian Brucker Mar 19 '18 at 14:20

1 Answers1

6

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 
Jimmy Lee Jones
  • 785
  • 4
  • 18