Flask Celery update_state from inside another function -
i'd update state of celery task function. here's have now:
the route
@app.route('/my-long-function', methods=['post']) def my_long_function():      param1 = request.form['param1']     param2 = request.form['param2']      task = outside_function.delay(param1, param2)      return task.id celery task - starts some_python_script.handle in background
@celery.task(name='outside_function') def outside_function(param1, param2):     app.app_context():         some_python_script.handle(param1, param2) some_python_script.handle:
def handle(param1, param2):     param1 + param2     # many, many different things ideally, i'd able self.update_state celery task can request status app, so:
some_python_script.handle (ideally):
def handle(param1, param2):     param1 + param2     # many, many different things     self.outside_function.update_state('progress', meta = {'status':'progressing'}) check progress (ideally):
@app.route('/status/<task_id>') def taskstatus(task_id):     task = outside_function.asyncresult(task_id)     response = {     'state': task.state,     'id': task.id,     'status' : task.status,     }      return jsonify(response) or similar. appreciate help, i'm new celery!
you should declare task id calling. can check update_state.
the below code should work.
# capture id of celery task id = self.request.id  def handle(param1, param2):     param1 + param2     # many, many different things     # update state of celery task direct reference     self.update_state(task_id=id, state='progress', meta = {'status':'progressing'}) 
Comments
Post a Comment