与Flask应用程序一起运行代码 [英] Run code alongside a Flask application
问题描述
我已经为我的python应用程序编写了一个Web界面.在运行 export FLASK_APP = main.py
然后运行 flask run
时,此功能可以很好地运行.现在,我希望实际的应用程序也能运行,因此该界面可能会很有用.
I've written a web interface for my python application. This functions beautifully when running export FLASK_APP=main.py
followed by flask run
. Now I want the actual application to also run, so the interface can be useful.
下面的代码是我的main.py,在这里我将烧瓶应用程序称为工厂函数.
The code below is my main.py, where I call the flask applications factory function.
from webinterface import create_app
if __name__ == '__main__':
create_app()
while(True):
# Some code logging different things
我想在无限循环内做一些事情,但是,当我尝试运行该应用程序时,它要么只运行Web界面,要么运行无限循环,具体取决于我是否使用 flask run
或 python main.py
.
I want to do stuff inside the infinite loop, however when I try to run the application, it either runs only the web interface or the infinte loop, depending if I start it using flask run
or python main.py
.
我如何最好地做到这一点?
How do I best accomplish this?
推荐答案
在前台应用程序的线程中运行Flask是可能的,并且有时很方便.有一个窍门,一个大陷阱和一个约束.
It's possible, and sometime convenient, to run Flask in a thread off of a foreground application. There is a trick, a big pitfall, and a constraint.
约束是,这是您要在安全"环境中执行的操作(例如,在便携式计算机上以本地浏览器为服务器,或在家庭Intranet上),因为这涉及到运行开发服务器,即您不想在敌对环境中做的事情.您也不能使用自动页面重新加载(但可以启用调试).
The constraint is that this is something you'll want to do in a "safe" environment (e.g. on your laptop to server a local browser, or on your home intranet), as it involves running the development server, which is something you don't want to be doing in a hostile environment. You also can't use automatic page reloading (but you can enable debug).
陷阱是,如果UI与前景应用程序共享任何不平凡的状态(包括字典),则需要使用共享的 threading.Lock()
来保护此类访问一次只有一个线程正在读取或写入数据.
The pitfall is that if the UI is sharing any non-trivial state (including dicts) with the foreground app, you'll need to use a shared threading.Lock()
to guard access such that only one thread at a time is reading or writing data.
诀窍是在创建共享状态之后但在启动之前,将对共享状态的引用注入到应用程序的配置中,例如:
The trick is to inject a reference to shared state into the app's config after you create it but before you start it, doing something like:
def webserver(state):
app.config['STATE'] = state
# If running on, say, a Raspberry Pi, use 0.0.0.0 so that
# you can connect to the web server from your intranet.
app.run(host='0.0.0.0', use_reloader=False, debug=True)
def main():
state = SharedState()
web_thread = threading.Thread(target=webserver, args=(state,))
web_thread.start()
state.set('counter' 0)
while True:
# Do whatever you want in the foreground thread
state.set('counter', state.get('counter') + 1)
class SharedState():
def __init__(self):
self.lock = threading.Lock()
self.state = dict()
def get(self, key):
with self.lock:
return self.state.get(key)
def set(self, key, value):
with self.lock:
self.state[key] = value
然后,在Flask视图功能中执行类似的操作
Then, from within Flask view functions, do something like
@app.route('/')
def home():
state = app.config['STATE']
counter = state.get(counter)
return render_template("index.html", counter=counter)
这篇关于与Flask应用程序一起运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!