[英]Run while loop concurrently with Flask server
I'm updating some LEDs using python. 我正在使用python更新一些LED。 I've been doing this like so:
我这样做是这样的:
from LEDs import *
myLEDs = LEDs()
done = False
while not done:
myLEDs.iterate()
I wanted to use Flask to act as a bridge between some nice-looking ReactJS front-end I can run in my browser (to change the current pattern, etc) and the LED-controlling code in Python. 我想使用Flask作为我可以在浏览器中运行的一些漂亮的ReactJS前端(用于更改当前模式等)和Python中的LED控制代码之间的桥梁。
I have Flask working fine, can handle HTTP requests, etc. I'm wondering how I can set myLEDs.iterate()
to continuously run (or run on a rapid schedule) concurrently with my flask app, while still being able to communicate with one another, like so: 我有Flask工作正常,可以处理HTTP请求等。我想知道如何设置
myLEDs.iterate()
以与我的烧瓶应用程序同时连续运行(或快速运行),同时仍然能够与彼此,像这样:
myLEDs = LEDs()
@app.route('/changePattern',methods=['POST'])
def changePattern():
n = request.json['num']
myLEDs.setPattern(n)
return jsonify(**locals())
if __name__ == '__main__':
app.debug = True
myLEDs.setToFrequentlyIterateAndStillTalkToFlask()
app.run()
I came across celery
, which seems like it would do the trick, but also seems like overkill for how simple my problem is. 我遇到了
celery
,看起来它可以做到这一点,但对于我的问题是多么简单来说似乎有些过分。
Is using Flask overkill for simply wanting a UI to manage my python back-end code? 使用Flask overkill只是想让UI管理我的python后端代码? Is there a simpler library than Celery to use for running something in the background?
是否有比Celery更简单的库用于在后台运行某些东西?
Edit 编辑
This is part of a larger project to develop an app with a Node-Webkit front-end attached to a Python backend. 这是开发一个带有连接到Python后端的Node-Webkit前端的应用程序的大型项目的一部分。 I'm open to changing my approach to this app if it doesn't seem feasible.
如果看起来不可行,我愿意改变我对这个应用程序的方法。
Use multiprocess to run the loop in a different process as the Flask HTTP requests: 使用multiprocess在Flask HTTP请求的不同进程中运行循环:
import time
from flask import Flask, jsonify
from multiprocessing import Process, Value
app = Flask(__name__)
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks})
def record_loop(loop_on):
while True:
if loop_on.value == True:
print("loop running")
time.sleep(1)
if __name__ == "__main__":
recording_on = Value('b', True)
p = Process(target=record_loop, args=(recording_on,))
p.start()
app.run(debug=True, use_reloader=False)
p.join()
The tasks part is from here , multiprocessing code from me. 任务部分来自这里 ,来自我的多处理代码。
Note the "use_reloader=False" part. 请注意“use_reloader = False”部分。 This is necessary to avoid running the loop twice.
这是避免两次运行循环所必需的。 For the reason see here
因为这里看到的原因
The functionality can be tested by starting up the server with 可以通过启动服务器来测试该功能
python <your_name_for_the example>.py
and calling 并打电话
curl -i http://localhost:5000/todo/api/v1.0/tasks
Although I am not the most qualified to comment here: I think this might help you. 虽然我不是最有资格在这里发表评论的人:我认为这可能会对你有所帮助。
How to run recurring task in the Python Flask framework? 如何在Python Flask框架中运行重复任务?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.