简体   繁体   English

如果进程已经在运行,Python-Flask 设置队列

[英]Python-Flask setting up a que if a process is already running

I have a python flask application where a person is gonna request data by sending a query from A form.我有一个 python flask 应用程序,其中一个人将通过从 A 表单发送查询来请求数据。 This data goes through a certain python script that does some api-requests and covert the data into a geographic standard.此数据通过某个 python 脚本执行一些 api 请求并将数据转换为地理标准。

The thing is because this data can take some time based on how many datapoints there are, this will happen in the background (we are researching this for Azure).问题是因为这些数据可能需要一些时间,具体取决于有多少数据点,这将在后台发生(我们正在为 Azure 进行研究)。 Also there is another problem, and that is cueing up.还有另一个问题,那就是提示。 Because if one request is running, another one cannot be started up.因为如果一个请求正在运行,另一个请求将无法启动。 And the last command cannot be saved:最后一条命令无法保存:

@app.route('/handle_data', methods=['POST'])
def handle_data():
    sper_year = int(request.form["Speryear"])
    email = request.form["inputEmail"]
    url = request.form["api-url-input"]
    random_string=get_random_string(5)
    # app.route('/request-completed')
    Requested_data = Program_Converter.main(url, sper_year,random_string)

Requested_data = Program_Converter.main(url, sper_year,random_string) is the function that needs to be qued. Requested_data = Program_Converter.main(url, sper_year,random_string) 是需要查询的function。

How do I do this?我该怎么做呢?

I believe that the most recommended way is to run this task asynchronously.我相信最推荐的方式是异步运行这个任务。 Take a look at Celery and pick up a backend (I recommend Redis), with this setup you can provide Celery a task that will run your GIPOD_Converter process in the background and store the result somewhere you chose, then it can be sent back to the user.查看 Celery 并选择一个后端(我推荐 Redis),通过此设置,您可以为 Celery 提供一个任务,该任务将在后台运行您的 GIPOD_Converter 进程并将结果存储在您选择的某个位置,然后可以将其发送回用户。

Note that Celery will provide you a task id and is up to your client (web interface or mobile app, I'm not sure what you're working with) to poll and endpoint and wait for the celery task to come to an end.请注意,Celery 将为您提供一个任务 ID,并由您的客户端(Web 界面或移动应用程序,我不确定您在使用什么)来轮询和终结并等待 celery 任务结束。

There are a couple of examples on who to implement this all over the web, but take a look at the Flask official documentation and check out this article from the mighty Miguel Grinberg , I believe those are the best starting points for you.关于在 web 中实现这一点的几个例子,但请查看Flask 官方文档并查看强大的Miguel Grinberg的这篇文章,我相信这些是您的最佳起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM