如何在烧瓶中的路线之间共享变量(Python)
我正在尝试使用会话在烧瓶中的路线之间共享变量。这是我正在使用的代码:
from flask import Flask, jsonify, request, session
from flask_cors import CORS
from flask_session import Session
app = Flask(__name__)
CORS(app)
SESSION_TYPE = 'filesystem'
app.config.from_object(__name__)
app.secret_key = 'abc'
@app.route("/getinputData", methods = ['POST'])
def getData():
inputData = json.loads(request.data)
molID = inputData["ID"]
searchType = inputData["searchType"]
session["sharedmolID"] = molID
session["sharedsearchType"] = searchType
return inputData
@app.route("/querydatabyMol", methods = ['GET'])
def querydatabyMol():
sharedmolID = session.get("sharedmolID", None)
sharedsearchType = session.get("sharedsearchType", None)
print(sharedmolID)
print(sharedsearchType)
return sharedmolID
if __name__ == '__main__':
app.run(debug=True)
sess = Session()
sess.init_app(app)
但是,当第二个路由打印变量时,我却一无所获。我已经使用全局变量暂时解决了此问题,但是我读到它们不是线程安全的,因此我想使用会话。
对这里有什么问题有任何想法吗?
I am trying to use sessions to share variables among routes in Flask. This is the code I am using:
from flask import Flask, jsonify, request, session
from flask_cors import CORS
from flask_session import Session
app = Flask(__name__)
CORS(app)
SESSION_TYPE = 'filesystem'
app.config.from_object(__name__)
app.secret_key = 'abc'
@app.route("/getinputData", methods = ['POST'])
def getData():
inputData = json.loads(request.data)
molID = inputData["ID"]
searchType = inputData["searchType"]
session["sharedmolID"] = molID
session["sharedsearchType"] = searchType
return inputData
@app.route("/querydatabyMol", methods = ['GET'])
def querydatabyMol():
sharedmolID = session.get("sharedmolID", None)
sharedsearchType = session.get("sharedsearchType", None)
print(sharedmolID)
print(sharedsearchType)
return sharedmolID
if __name__ == '__main__':
app.run(debug=True)
sess = Session()
sess.init_app(app)
But when the second route prints the variables I get None. I have temporarily solved this using global variables, but I read that they are not thread safe, so I would like to use sessions.
Any idea of what is wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用以下方式替换“ session.get()”方法:
另请参见有关一个相关问题
You could try replacing the "session.get()" Method with:
also see this for a related problem