为了账号安全,请及时绑定邮箱和手机立即绑定

在浏览器中保持 Flask 在没有网页的情况下运行

在浏览器中保持 Flask 在没有网页的情况下运行

米琪卡哇伊 2022-10-08 15:13:34
我有一个处理传感器数据的本地烧瓶服务器。当温度大于某个阈值时,它会获取温度值并调整灯光。到目前为止,它仅在一个客户端保持浏览器打开该页面时才有效。如果服务器可以在不一直打开浏览器的情况下更新温度值并调整光线,那就太好了。如何让它在后台运行?temperture = 0threshold_temp = 0def read_temp():    #read temperature valuesserver = Flask(__name__)socketio = SocketIO(server)app = dash.Dash(__name__, server = server, url_base_pathname="/dash/")@server.route('/_stuff')def stuff():    global threshold_temp    temperature = read_temp()    if temperature >= threshold_temp:            #change light    else:            #do not change light    return jsonify(result = temperature)#Change Threshold@socketio.on('message')def handleMessage(msg):    global threshold_temp    threshold_temp = float(msg)    send(msg, broadcast=True)    f = open("Threshold.txt", "w")    f.write(msg)    f.close()#Adjust Threshold on Connection of Client@socketio.on('connect')def on_connect():    global threshold_temp    f = open("Threshold.txt", "r")    threshold_temp = float(f.read())    send(threshold_temp, broadcast=True)@server.route('/')def index():    return render_template('index.html')if __name__ == '__main__':    socketio.run(server, host='0.0.0.0', port=80, debug=False)
查看完整描述

1 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

听起来你想运行一个后台任务;消息代理是处理这类事情的好方法。python-rqcelery是很好的选择。



查看完整回答
反对 回复 2022-10-08
  • 1 回答
  • 0 关注
  • 116 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号