我有一個處理傳感器數(shù)據(jù)的本地?zé)糠?wù)器。當(dāng)溫度大于某個閾值時,它會獲取溫度值并調(diào)整燈光。到目前為止,它僅在一個客戶端保持瀏覽器打開該頁面時才有效。如果服務(wù)器可以在不一直打開瀏覽器的情況下更新溫度值并調(diào)整光線,那就太好了。如何讓它在后臺運行?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)
在瀏覽器中保持 Flask 在沒有網(wǎng)頁的情況下運行
米琪卡哇伊
2022-10-08 15:13:34
