【问题标题】:Keep Flask Running Without Web Page in Browser在浏览器中保持 Flask 在没有网页的情况下运行
【发布时间】:2020-10-07 12:06:06
【问题描述】:

我有一个处理传感器数据的本地烧瓶服务器。当温度大于某个阈值时,它会获取温度值并调整灯光。到目前为止,它仅在一个客户端保持浏览器打开该页面时才有效。如果服务器在不一直打开浏览器的情况下更新温度值并调整光线,那就太好了。如何让它在后台运行?

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)

【问题讨论】:

  • 您可以使用调度程序并安排它每秒运行一次。参考this
  • 谢谢!这就是我一直在寻找的

标签: javascript python flask server


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2012-07-06
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多