问题
I am working on building a gui/dashboard type of thing thru which I take input from user.. and when user press submits.. I gather response and fire a job in back end. What I am hoping to achive is.. When the user press submits: and that long job is being processed, I show something like: "Your job has been submitted succesfully) submitted and when it is finished.. I refresh the page to take user to that page.
Here is how my route.py snippet looks like
@app.route('/',methods=['POST'])
def get_data():
data = request.form
for data_tuple in data:
requests_to_gb[data_tuple] = data[data_tuple]
flag = execute_request(requests_to_gb) <---Fires a job
if flag:
flash("Your request has been submitted")
else:
flash("request could not be completed!")
return render_template('request_submitted.html')
But the issue is.. that line where i execute_request() takes a long time to process.. and everything is halted until that is finished? How do I handle this? And how do i automatically refresh to new page as well?
回答1:
Use celery which is a distributed task queue. The quickstart guide should get you going.
In a nutshell, it allows you to offload tasks to workers that run in the background so that you don't block your main user interface (which is what you are trying to prevent).
The good news is that it is very easy to integrate with flask (or django, or anything else really) since its written in Python.
来源:https://stackoverflow.com/questions/14904523/how-to-execute-long-requests