Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery task timeout/time limit for windows?

I have a web app written in Flask that is currently running on IIS on Windows (don't ask...).

I'm using Celery to handle some asynchronous processing (accessing a slow database and generating a report).

However, when trying to set up some behavior for error handling, I came across this in the docs:

"Time limits do not currently work on Windows and other platforms that do not support the SIGUSR1 signal."

Since the DB can get really slow, I would really like to be able to specify a timeout behavior for my tasks, and have them retry later when the DB might not be so tasked. Given that the app, for various reasons, has to be served from Windows, is there any workaround for this?

Thanks so much for your help.

like image 313
Akrasiac Avatar asked Jun 10 '13 21:06

Akrasiac


1 Answers

If you really need to set the task timeout, you can use the child process to achieve, the code as follows

import json
from multiprocessing import Process
from celery import current_app
from celery.exceptions import SoftTimeLimitExceeded

soft_time_limit = 60

@current_app.task(name="task_name")
def task_worker(self, *args, **kwargs):
    def on_failure():
        pass
    worker = Process(target=do_working, args=args, kwargs=kwargs, name='worker')
    worker.daemon = True
    worker.start()
    worker.join(soft_time_limit)
    while worker.is_alive():
        worker.terminate()
        raise SoftTimeLimitExceeded  
    return json.dumps(dict(message="ok"))

def do_working(*args, **kwargs):
    pass # do something
like image 161
PandllCom Avatar answered Sep 30 '22 10:09

PandllCom