pythoncelerycelery-task

How to set time limits on tasks using task_annotations with Celery


I'm trying to get Celery to enforce time limits on tasks using task_annotations within the app configuration. This seems to be possible according as exemplified in docs and specified here. However, I was unable to apply this, the example below shows a task that should take longer than the timeout but that will successfully complete.

in simple.py:

from celery import Celery, shared_task
from time import sleep

class Config:
#    task_time_limit = 1 # this is commented out but otherwise it would enforce the time limit
    task_annotations = {'*': {'task_time_limit': 1}} # this is where I expected the time limit to work
    
app = Celery('tasks')

app.config_from_object(Config)
    
@shared_task(name='run_task') # I'm aware that I can set task_time_limit in this decorator
def run_task():
    sleep(5)
    print('Completed without timing out :(')

you can run this with celery -A simple worker -l info

one can trigger this task in another shell with python -c "from simple import run_task; run_task.delay()". This expects RabbitMQ to be running on the same machine.

How can I change the task_annotations specified in the config in order to apply task time limit and what key (other than '*') should I use to assign it specifically to my task?

Thanks in advance.


Solution

  • Turns out that 'time_limit' works here as opposed to 'task_time_limit'.

    task_annotations = {'run_task': {'time_limit': 1}}

    Not sure if this is an issue with the documentation or if I missed something but its settings table misled me.