Flask 会话成员不会跨请求持续存在 [英] Flask session member not persisting across requests
问题描述
我正在编写一个快速应用程序来查看一个巨大的 XML 文件,其中包含对 viewgroup
的一些 AJAX 样式调用.我的问题是 session['groups']
不持久.我有一些只有 4 个成员的旧数组被卡在某个地方(cookie?..).该值在调用 view
时出现.然后我用最近打开的包含 20 多个成员的 xml 文件中的信息覆盖该会话成员.
I'm writing a quick app to view a giant XML file with some AJAX style calls to viewgroup
. My problem is session['groups']
not persisting. I have some old array with only 4 members that is stuck somewhere (cookie?..). That value is present when view
is called. I then overwrite that session member with info from the recently opened xml file that contains 20+ members.
但是,当调用 viewgroup
时,会话变量已恢复为旧值,数组中只有 4 个成员!
However, when viewgroup
is called the session variable has reverted to the old value with only 4 members in the array!
代码后跟输出.注意 3 个 sessionStatus()
调用
Code followed by output. Note the 3 sessionStatus()
calls
def sessionStatus():
print "# of groups in session = " + str(len(session['groups']))
@app.route('/')
def index():
cams = [file for file in os.listdir('xml/') if file.lower().endswith('xml')]
return render_template('index.html', cam_files=cams)
@app.route('/view/<xmlfile>')
def view(xmlfile):
path = 'xml/' + secure_filename(xmlfile)
print 'opening ' + path
xmlf = open(path, 'r')
tree = etree.parse(xmlf)
root = tree.getroot()
p = re.compile(r'Group')
groups = []
for g in root:
if (p.search(g.tag) is not None) and (g.attrib['Comment'] != 'Root'):
groups.append(Group(g.attrib['Comment']))
sessionStatus()
session['groups'] = groups
sessionStatus()
return render_template('view.html', xml=xmlfile, groups=groups)
@app.route('/viewgroup/<name>')
def viewGroup(name):
groups = session['groups']
sessionStatus()
if groups is None or len(groups) == 0:
raise Exception('invalid group name')
groups_filtered = [g for g in groups if g.name == name]
if len(groups_filtered) != 1:
raise Exception('invalid group name', groups_filtered)
group = groups_filtered[0]
prop_names = [p.name for p in group.properties]
return prop_names
输出
opening xml/d.xml
# of groups in session = 5
# of groups in session = 57
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /view/d.xml HTTP/1.1" 200 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/ivtl.css HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/jquery.js HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/raphael-min.js HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/ivtl.css HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /favicon.ico HTTP/1.1" 404 -
# of groups in session = 5
127.0.0.1 - - [17/Aug/2011 17:27:31] "GET /viewgroup/DeviceInformation HTTP/1.1" 200 -
我需要所有 57 个小组都留下来.有什么提示吗?
I need all 57 groups to stay around. Any hints?
推荐答案
数据太大而无法序列化到会话中.现在我在全局字典中生成一个密钥并将该密钥存储在会话中.
The data was simply too big to serialize into the session. Now I generate a key into a global dict and store that key in the session.
gXmlData[path] = groups
存在的问题是全局字典会随着越来越多的键而永远存在,但这个过程并不意味着长久存在.
There's the problem that the global dict will stay around forever with more and more keys but the process isn't meant to live long.
这篇关于Flask 会话成员不会跨请求持续存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!