在浏览器中保持 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)


米琪卡哇伊
浏览 117回答 1
1回答

有只小跳蛙

听起来你想运行一个后台任务;消息代理是处理这类事情的好方法。python-rq和celery是很好的选择。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript