【问题标题】:Running a continous while loop in python along with Flask app在 python 中与 Flask 应用程序一起运行一个连续的 while 循环
【发布时间】:2015-11-09 18:57:18
【问题描述】:

我正在使用 python 在树莓派中编写代码,我希望用户通过网页输入设定温度和风扇模式,我正在使用烧瓶,并且值成功返回,但我也想运行无限循环与烧瓶应用程序一起将设定温度与传感器的当前温度进行比较..我如何在不中断烧瓶应用程序的情况下实现这一点?

from flask import Flask
from flask import render_template
from flask import request
from flask import redirect
import time
temp = ""
t = ""
fan_High = 0
fan_Med = 0
fan_Low =0
fanspeed = ""


app = Flask(__name__)


@app.route('/form', methods=['POST'])
def aziz():
    global temp ,fanspeed
    fanspeedlocal = ''
    if request.form['settemp'] != "":
        temp = request.form['settemp']
        templocal = temp
    else:
        templocal = temp


    if request.form['speed'] == "null":
        fanspeedlocal = fanspeed
    else:
        if request.form['speed'] == "High":
            fan_Med = False
            fan_Low = False
            fan_High = True
            fanspeed = "High"
            fanspeedlocal = fanspeed
        elif request.form['speed'] == "Med":
            fan_High = False
            fan_Low = False
            fan_Med = True
            fanspeed = "Medium"
            fanspeedlocal = fanspeed
        elif request.form['speed'] == "Low":
            fan_High = False
            fan_Med = False
            fan_Low = True
            fanspeed = "Low"
            fanspeedlocal = fanspeed
    print 'Settemp = %s' %temp
    print 'fanspeed = %s' %fanspeed
    return render_template('Output.html',temp=templocal,currtemp=strct,time=t,fanspeed=fanspeedlocal




@app.route('/')
def start():
    global t , fanspeed
    t = time.strftime("%H:%M:%S")    
    return render_template('Start.html',temp=temp,currtemp=strct,time=t,fanspeed=fanspeed)




if __name__ == '__main__':
    app.debug = False
    app.run(host = '192.168.1.101')


var = 1
while var == 1:
    inttemp = int(temp)
    if currtemp >= inttemp:
            #set GPIO to high
    else:
            #set GPIO to low

    if fanspeed == 'High':
            #set GPIO to high
    elif fanspeed == 'Med':
            #set GPIO to high
    elif fanspeed == 'LOW':
            #set GPIO to high
    time.sleep(10)

【问题讨论】:

  • 使用threading。它非常复杂,但如果你不知道你用它做什么。
  • 我不会说它非常复杂..你只需要了解它。
  • 线程似乎是一个解决方案..你能发布一个简单的例子吗?
  • 而且您可能应该使用比简单的无限 while-loop 更好的东西,因为它会最大化您的 RPI 的整个核心。最好使用偶尔唤醒一次的线程。
  • thanx 伙计们的快速回复.. 我在最后一个 while 循环中附加了代码,我需要与烧瓶应用程序一起运行。

标签: python loops while-loop flask


【解决方案1】:

我想,你只要在里面输入while命令:

@app.route('/')
def start():

或者在你喜欢的地方

【讨论】:

    【解决方案2】:

    我只会使用 cron。您可以使用如下所示的简单脚本:

    #!/usr/bin/python3 
    # or python, if that's your thing
    
    import requests
    
    def get_sensor_data():
        '''Presumably you know what actually goes here.'''
        return {'temperature': 5, 'fan speed': 'no, three, sir'}
    
    requests.post('http://example.com/update?api_key=12345', data=get_sensor_data())
    

    然后只需将 cron 设置为每 60 秒或更短时间运行一次。现在,您的 Flask 应用程序只会从您的脚本获取更新数据的请求,同时您的页面也会更新。

    或者,您可以设置一个 Flask @app.before_request decorator,它只会请求必要的数据并将其附加到特殊的 g 对象。

    如果读取数据的速度很快(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2018-09-06
      • 2014-01-09
      • 2020-12-27
      • 1970-01-01
      相关资源
      最近更新 更多