Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

flask在使用多线程后,每个线程中不同路由之间如何共享参数?

之前是使用了一个全局变量来在不同路由之间共享参数,想请教下在开了多线程后,该如何做呢?

@app.route('/predict', methods=['POST'])
def model_predict():
        global res_dict
        res_dict = {}
           .....


@app.route('/hotel_summary')
def hotel_summary():
    hotel_name = res_dict["hotel_name"]

           .....

在用gunicorn开了多线程之后,当访问hotel_summary的路由时,直接报错“NameError: name 'res_dict' is not defined”
想请教下各位大佬该如何解决这个问题呢?万分感谢!!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你应该将这些数据存入DB或者Cache。Web服务最好做成无状态的,不然你后续可能又要问如何跨进程传递参数,如何跨机器传递参数……还有,将数据暂存在web服务进程内部,服务一重启就啥都没了……


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...