How to execute long requests

痴心易碎 提交于 2021-02-08 11:27:55

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!