Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Time Limit on specific task with celery

Tags:

celery

celeryd

I have a task in Celery that could potentially run for 10,000 seconds while operating normally. However all the rest of my tasks should be done in less than one second. How can I set a time limit for the intentionally long running task without changing the time limit on the short running tasks?

like image 302
jimstandard Avatar asked Jul 26 '12 14:07

jimstandard


People also ask

How do you set time on Celery?

To schedule task you need to use celery beat . You can schedule your task at any specific time using periodic task . To know more use this link https://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html . Don't forget to restart your celery beat after creation of task .

What is a soft time limit?

The soft time limit allows the task to catch an exception to clean up before it is killed: the hard timeout isn't catch-able and force terminates the task.

How do you stop a celery task?

To cancel an already executing task with Celery and Python, we can use the revoke function. to call revoke with the task_id of the task to stop. And we set terminate to True to terminate the task.


2 Answers

You can set task time limits (hard and/or soft) either while defining a task or while calling.

from celery.exceptions import SoftTimeLimitExceeded  @celery.task(time_limit=20) def mytask():     try:         return do_work()     except SoftTimeLimitExceeded:         cleanup_in_a_hurry() 

or

mytask.apply_async(args=[], kwargs={}, time_limit=30, soft_time_limit=10) 
like image 94
mher Avatar answered Sep 19 '22 08:09

mher


This is an example with decorator for an specific Task and Celery 3.1.23 using soft_time_limit=10000

@task(bind=True, default_retry_delay=30, max_retries=3, soft_time_limit=10000) def process_task(self, task_instance):    """Task processing."""         pass 
like image 34
gogasca Avatar answered Sep 21 '22 08:09

gogasca