在Python / Flask中动态地在循环中使用全局变量的更新值

时间:2018-10-15 12:51:39

标签: python flask global-variables

我正在尝试构建一个小的Flask应用程序,在该应用程序中,我尝试从HTML页面中“发布”一些变量的值,并在全局字典global_dict中对其进行更新,然后在生成器方法中使用它们。问题出在生成器gen(m2I)内部,而循环时全局值未更新。我想要的是,无论何时更改这些变量smv1v2v3的值,都希望从HTML页面中将它们反映在while循环。以下是代码的相关部分。有什么想法可以做到吗?

app.route('/')
def index():
    if request.method=='POST':
        return redirect(url_for('index'))
    return render_template('index.html')

def gen(m2I):
    while True:
        global global_dict
        sm, m1, m2 = global_dict['sm'], global_dict['m1'], global_dict['m2']
        v1, v2, v3 = global_dict['v1'], global_dict['v2'], global_dict['v3']
        print('here2',sm, m1, m2, v1, v2, v3)
        something = m2I.get_something(s1=sm, ap=ma, am=mma, vp=v1, vm=v2, vstd=v3)
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route('/video_feed', methods=['GET', 'POST'])
def video_feed():

    if request.form.get('va'):
        global global_dict
        global_dict['m1'], global_dict['m2'] = float(request.form['m1']), float(request.form['m2'])
        global_dict['v1'], global_dict['v2'] = float(request.form['v1']), float(request.form['v2'])
        global_dict['v3'], global_dict['sm'] = float(request.form['v3']), float(request.form['sm'])   


    return Response(gen(m2I), mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(debug=True)

0 个答案:

没有答案