首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >烧瓶:如何以post方法作为后台任务运行函数,然后在另一个类中访问它

烧瓶:如何以post方法作为后台任务运行函数,然后在另一个类中访问它
EN

Stack Overflow用户
提问于 2018-04-06 08:44:00
回答 1查看 1.6K关注 0票数 0

我是自动化的泵,只有当土壤湿度值(从土壤湿度传感器获得)超过某一值时才开启。情况是这样的:

  1. 用户从下拉列表中选择一个值(土壤湿度的阈值)。
  2. python脚本不断检查来自传感器的值是否大于下拉列表中选择的值(这就是为什么我需要让它作为后台任务运行)。
  3. 用户可以通过使用网页上的“关闭”按钮来关闭后台任务来关闭自动化过程。

我尝试了以下操作,但得到了working outside of request context错误:

web_plants.py(which调用python soil_on.py脚本):

代码语言:javascript
运行
复制
@app.route("/threshold", methods=['POST', 'GET'])
def threshold():
    tvalue= -1 #get value from dropdown
    msg = ''
    if request.method == "POST":
           msg= "rating above 3"
           os.system("python soil_on.py&")
    templateData = template(text = msg) #display text using template()
    return render_template('index.html', **templateData)

soil_on.py(which从water类调用auto_Irrigation()函数:

代码语言:javascript
运行
复制
import water

if __name__ == "__main__":
    water.auto_Irrigation()

water.py (有auto_Irrigation())

代码语言:javascript
运行
复制
app = Flask(__name__)

def template(title = "HELLO!", text = ""):
templateDate = {
    'text' : text,
    'tvalues' : getTValues(),
    'selected_tvalue' : -1
}
return templateDate

def getTValues():
  return (10, 11, 15, 2, 1)

@app.route("/", methods=['POST', 'GET'])
def auto_Irrigation():
    tvalue= -1 #get value from dropdown
   # msg = ''
    if request.method == "POST":
        tvalue = int(request.form['tvalue'])
        if tvalue> 3:
            GPIO.output(40, GPIO.HIGH)
        else:
           GPIO.output(40, GPIO.LOW)
    return render_template('index.html', **templateData)

index.html

代码语言:javascript
运行
复制
<h2> {{text}} </h2>
<form action= "{{ url_for('threshold') }}" method="POST">

     <select name= 'tvalue'>
      {% for tvalue in tvalues %}
        {% if selected_tvalue == tvalue %}
            <option value="{{ tvalue }}" selected='selected'>{{ tvalue }}</option>
        {% else %}
             <option value="{{ tvalue }}" >{{ tvalue }}</option>
        {% endif %}
      {% endfor %}

      </select>

     <input type="submit" value="Submit" />
</form>

我不知道如何使这个功能成为一个背景任务。目前,这是为我工作,但该函数没有作为后台任务运行(因此它只检查值和打开/关闭泵):

index.html

代码语言:javascript
运行
复制
<h2> {{text}} </h2>
<form action= "{{ url_for('threshold') }}" method="POST">

     <select name= 'tvalue'>
      {% for tvalue in tvalues %}
        {% if selected_tvalue == tvalue %}
            <option value="{{ tvalue }}" selected='selected'>{{ tvalue }}</option>
        {% else %}
             <option value="{{ tvalue }}" >{{ tvalue }}</option>
        {% endif %}
      {% endfor %}

      </select>

     <input type="submit" value="Submit" />
</form>

为了这篇文章,web_plants.py:(simplified函数threshold()使它更容易理解)

代码语言:javascript
运行
复制
def template(title = "HELLO!", text = ""):
    templateDate = {
        'text' : text,
        'tvalues' : getTValues(),
        'selected_tvalue' : -1
    }
    return templateDate

def getTValues():
    return (10, 11, 15, 2, 1) 

@app.route("/threshold", methods=['POST', 'GET'])
def threshold():
    tvalue= -1 #default value
    msg = ''
    if request.method == "POST":            
        tvalue = int(request.form['tvalue'])
        if tvalue> 3:
            msg= "rating above 3"

    #generating template data
    templateData = template(text = msg)
    templateData['selected_tvalue'] = tvalue 

    return render_template('index.html', **templateData)
EN

回答 1

Stack Overflow用户

发布于 2018-04-06 08:48:35

考虑使用芹菜,这是一个异步任务队列。继续运行长期运行的作业,获取状态更新(甚至是自定义更新)。

这些链接应该可以帮助您理解它是如何工作的:

https://github.com/miguelgrinberg/flask-celery-example

https://blog.miguelgrinberg.com/post/using-celery-with-flask

你甚至可以看看芹菜的文件。

这是如何实现您的函数的示例。你可以从那里继续前进。

/tasks.py

代码语言:javascript
运行
复制
@celery.task
def my_task(self, threshold):
    if sensor_val < threshold:
        self.update_state(state="Sensor Value below threshold", meta={'sensor_val':sensor_val, 'threshold':threshold})
    else:
        self.update_state(state="Sensor Value past threshold. Please check.", meta={'sensor_val':sensor_val, 'threshold':threshold})
        """Do whatever you would like to do"""

/views.py

代码语言:javascript
运行
复制
def sensor_check(request):
    if request.method == "POST":
        threshold = request.POST['threshold']
        from tasks import my_task
        job = my_task.delay(threshold)
        return HttpResponseRedirect(reverse("task_status")+"?job_id="+job.id)

def task_status(request):
    if 'job_id' in request.GET:
        job_id = request.GET['job_id']
        job = AsyncResult(job_id)
        data = job._get_task_meta()
        return JsonResponse(data)
    else:
        JsonResponse("No job ID given")
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49688824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档