I have a Celery task that runs the main
function of external Python scripts. However, some of the scripts may take a very very long time to execute and I'd like to set a time limit after which the tasks are aborted if they're not finished.
I've tried all the ways I've found on other similar questions like this one but none of the things I tried worked.
My task is the following :
@app.task(name="execute_script", bind=True, base=CallbackTask)
def execute_script(self, script_id, variables, name, start_time):
self.update_state(state='EXECUTING')
script = Script.objects.get(pk=script_id)
mod = Script.get_script(script) # Getting the script's module
try:
mod.main(variables, name)
except Exception as e:
raise e
return name
And my Celery settings :
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')
app = Celery('main', backend='django-db')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
Is there a way to set a time limit on a Celery task after which it's aborted if not finished ?