Flask Celery update_state from inside another function Flask Celery update_state from inside another function flask flask

Flask Celery update_state from inside another function


You should declare the task id for calling.You can check update_state.

The below code should work.

# capture id of celery taskID = self.request.iddef handle(param1, param2):    param1 + param2    # many, many different things    # update the state of celery task with direct reference to it    self.update_state(task_id=ID, state='PROGRESS', meta = {'status':'progressing'})


If you add bind=True to the main celery task, you get access to the celery task object with the self keyword. Pass the task object to the next function using self.

@celery.task(name='outside_function', bind=True)def outside_function(self, param1, param2):    with app.app_context():        some_python_script.handle(self,param1, param2)

The other function can accept this task object and update it:

def handle(celery_task, param1, param2):    param1 + param2    # many, many different things    celery_task.update_state('PROGRESS', meta = {'status':'progressing'})