I'm learning to use celery. I have a function (in a Flask application) in somewhat pseudocode from what I've learned so far:
def do_a_task_route():
try:
result = a_celery_task.apply_async(args=[request_data])
returned = a_celery_task.AsyncResult(result.task_id).get(timeout = 2.0)
return jsonify(response = returned['response'])
except:
return jsonify(response = "some big problem")
I'm not 100% sure this right and best, but works for me now. It seems that the result/returned lines could be one "returned = run_task" where run task does task and waits.
I was thinking on refactoring directions -- a) bundle those two lines (result/returned) in a function that runs a task and call the result or b) put the "AsyncResult" into the task itself and have that return.
What is the celery way from someone who has done this before? Or just some feedback from someone who has fine tuned celery before.