调用Flask app.run之后,我可以让Python代码继续执行吗? [英] Can I have Python code to continue executing after I call Flask app.run?

查看:226
本文介绍了调用Flask app.run之后,我可以让Python代码继续执行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Python,尽管过去30年来我一直在用其他语言进行编程.我想使我的第一个应用程序保持简单,因此我从Raspberry Pi上托管的一个小型家庭自动化项目开始.

I have just started with Python, although I have been programming in other languages over the past 30 years. I wanted to keep my first application simple, so I started out with a little home automation project hosted on a Raspberry Pi.

我的代码可以正常工作(控制阀,读取流量传感器并在显示屏上显示一些数据),但是当我想添加一些Web交互性时,它突然停顿了.我在该主题上发现的大多数文章都建议使用Flask框架来组成动态网页.我已经尝试并理解了Flask的基础知识,但是一旦我调用"app.run"函数,就无法解决Flask阻塞的问题.我的python代码的其余部分等待Flask返回,但从未发生.IE.不再进行水流量测量,阀马达转向或显示更新.

I got my code to work fine (controlling a valve, reading a flow sensor and showing some data on a display), but when I wanted to add some web interactivity it came to a sudden halt. Most articles I have found on the subject suggest to use the Flask framework to compose dynamic web pages. I have tried, and understood, the basics of Flask, but I just can't get around the issue that Flask is blocking once I call the "app.run" function. The rest of my python code waits for Flask to return, which never happens. I.e. no more water flow measurement, valve motor steering or display updating.

因此,我的基本问题是:为了与我的应用程序主要任务(GPIO/脉冲计数)并行地服务简单的动态网页(负载非常低,如每周1次请求),我应该使用哪种工具?)?所有这些都是在Raspberry Pi(3)的资源受限的环境中进行的.如果您仍然建议Flask(因为它似乎非常接近目标),那么我应该如何安排我的代码以继续处理诸如上述的现实事件?

So, my basic question would be: What tool should I use in order to serve a simple dynamic web page (with very low load, like 1 request / week), in parallel to my applications main tasks (GPIO/Pulse counting)? All this in the resource constrained environment of a Raspberry Pi (3). If you still suggest Flask (because it seems very close to target), how should I arrange my code to keep handling the real-world events, such as mentioned above?

(这最后一部分可能很难看清楚实际的代码,但是也许可以通用"的方式回答?或者指向我在搜索时可能会遗漏的现有示例.)

(This last part might be tough answering without seeing the actual code, but maybe it's possible answering it in a "generic" way? Or pointing to existing examples that I might have missed while searching.)

推荐答案

使用多线程技术正处于正确的轨道.如果您的监视代码循环运行,则可以定义类似

You're on the right track with multithreading. If your monitoring code runs in a loop, you could define a function like

def monitoring_loop():
    while True:
        # do the monitoring

然后,在调用 app.run()之前,启动一个运行该功能的线程:

Then, before you call app.run(), start a thread that runs that function:

import threading
from wherever import monitoring_loop

monitoring_thread = threading.Thread(target = monitoring_loop)
monitoring_thread.start()

# app.run() and whatever else you want to do

不要 join 线程-您希望它继续与Flask应用程序并行运行.如果您加入了它,它将阻塞主执行线程,直到完成为止,因为它运行的是 while True 循环,所以永远不会执行.

Don't join the thread - you want it to keep running in parallel to your Flask app. If you joined it, it would block the main execution thread until it finished, which would be never, since it's running a while True loop.

要在监视线程和程序的其余部分之间进行通信,可以使用

To communicate between the monitoring thread and the rest of the program, you could use a queue to pass messages in a thread-safe way between them.

这篇关于调用Flask app.run之后,我可以让Python代码继续执行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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