Context: I built a web app using flask to make several calls to an API and create/download the results of those calls to an excel file on the fly. To avoid timing out on larger jobs, I implemented redis with RQ to handle the work in the background. This is all up and running, however neither send_file() nor send_from_directory() are actually sending the file (I was using send_file() before implementing rq and it was working without problem). The file is built and returns a 200 status, but the download doesn't start automatically. If I right click on the call and open in a new tab, the file downloads.
Here is some of my code:
@app.route("/status/<job_id>", methods=['GET'])
def job_status(job_id):
job = queue.fetch_job(job_id)
if job.is_finished:
output = make_xls()
output.seek(0)
filename = '{}_{}.xlsx'.format(session['filter'], session['id'])
return send_file(output, mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",attachment_filename=filename, as_attachment=True)
I have ajax polling the this route continuously so to know when the job is finished.
make_xls() returns a io.BytesIO() used by pandas.ExcelWriter() to create the excel.
I'm using gevent as the worker in gunicorn so the job is run asynchronously. Let me know what other information I can provide. There isn't a traceback, that I can see, it seems like everything runs successfully, just no file is downloaded. I've also tried saving the file locally then making a GET to another route with the file name and then trying to return the file with send_from_directory(). The request returns 200. And if I right-click and open in new tab, the file downloads.
I've been reading up on race conditions and blocking vs non-blocking, but just haven't been able to figure out what I'm missing. Any help would be appreciated, even if it is things to look in to.
Thanks