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

Javascript 保持Flask在浏览器中无网页的情况下运行,javascript,python,flask,server,Javascript,Python,Flask,Server,我有一个处理传感器数据的本地flask服务器。当温度高于某个阈值时,它获取温度值并调整灯光。到目前为止,它只在一个客户端保持浏览器打开该页面时起作用。如果服务器可以更新温度值并调整灯光,而不必一直打开浏览器,那就太好了。如何让它在后台运行 temperture = 0 threshold_temp = 0 def read_temp(): #read temperature values server = Flask(__name__) socketio = SocketIO(ser

我有一个处理传感器数据的本地flask服务器。当温度高于某个阈值时,它获取温度值并调整灯光。到目前为止,它只在一个客户端保持浏览器打开该页面时起作用。如果服务器可以更新温度值并调整灯光,而不必一直打开浏览器,那就太好了。如何让它在后台运行

temperture = 0
threshold_temp = 0

def read_temp():
    #read temperature values

server = 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)

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

您可以使用调度程序,并将其安排为每秒运行一次。参考,谢谢!这就是我要找的