如何执行长请求 [英] How to execute long requests

查看:49
本文介绍了如何执行长请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个gui/dashboard类型的东西,通过该东西我可以从用户那里获取输入信息.当用户按下提交时,我可以收集响应并在后端执行工作.我希望达到的目标是..当用户按下提交时:并且正在处理长时间的工作,我会显示类似以下内容:您的工作已成功提交)已提交并在完成时..我刷新页面以将用户带到该页面.

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.

这是我的route.py片段的样子

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')

但是问题是..我execute_request()需要花费很长时间处理的那一行..一切都停止了,直到完成为止?我该如何处理?以及如何自动刷新到新页面?

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?

推荐答案

使用 芹菜 是分布式任务队列.快速入门指南应该可以助您一臂之力

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).

好消息是,自烧瓶(或django或其他任何东西)以来,它很容易与Python集成.

The good news is that it is very easy to integrate with flask (or django, or anything else really) since its written in Python.

这篇关于如何执行长请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆