I am trying to implement a large file upload in flask
using celery
. I have a function that saves the file stream to the disk as a celery task but, my implementation requires the use of the request
context inside of my task function. I have done my research and implemented the solutions provided on the links given on this answer but, this doesn't seem to work with my version of celery which is 4.1.0
Here is what I am doing
This is my task
@task(name="app.save_operation")
def save_operation():
content = request.files["content"]
content.save(
content.filename)
content.stream.close()
return
This is the @task
wrapper
def task(**kwargs):
def decorator(func):
@celery_helper.task(**kwargs)
@functools.wraps(func)
def wrapped(*args, **kwargs):
with app.test_request_context():
return func(*args, **kwargs)
return wrapped
return decorator
And this is the API handler function where I am calling this task
@app.route("/upload", methods=["GET", "POST"])
def upload():
if (request.method == "POST"):
filename = request.files["content"].filename
task_id = save_operation.delay()
return "upload started for "+filename
``
This should work fine in theory but, when I do this, celery throws a BadRequestKeyError
which essentially means that the request
object which is being referred in the save_operation()
task is not being passed properly. I am not able to figure out why this is happening and I have reached a roadblock with my research.
I would really like some insights on this.