Flask会话成员不会持续请求 [英] Flask session member not persisting across requests
问题描述
我正在写一个快速的应用程序来查看一个巨大的XML文件,其中有一些AJAX风格的调用 viewgroup
。我的问题是 session ['groups']
不能持续。我有一些只有4个成员的旧数组卡在某处(cookie?..)。当 view
被调用时,该值是存在的。然后,我用最近打开的包含20多个成员的xml文件中的信息覆盖该会话成员。但是,当调用 viewgroup
时,会话变量已经恢复到旧值,只有4个成员在数组中!
输出代码。注意3 sessionStatus()
调用
def sessionStatus b $ b printsession =+ str(len(session ['groups']))
$ b @ app.route('/')
def index() :
cams = [file.lower()。endswith('xml')]
返回render_template('index.html',cam_files =凸轮)
@ app.route('/ view /< xmlfile>')
def view(xmlfile):
path ='xml /'+ secure_filename(xmlfile)
print'opening'+ path
xmlf = open(path,'r')
tree = etree.parse(xmlf)$ b $ root root = tree.getroot()$ b $如果(p.search(g.tag)不是None)和(g .group')
groups = []
, attrib ['Comment']!='Root'):
groups.append(Group(g.attrib ['Comment']))
sessionStatus()
session ['groups'] =组
sessionStatus()
返回render_tem ('view.html',xml = xmlfile,groups = groups)
@ app.route('/ viewgroup /< name>)
def viewGroup(name):
groups = session ['groups']
sessionStatus()
如果groups是None或len(groups)== 0:
抛出Exception('invalid group name')
groups_filtered = [g for g in groups if g.name == name]
if len(groups_filtered)!= 1:
异常('invalid group name',groups_filtered)
group = groups_filtered [0]
prop_names = [p在group.properties中的p.name]
返回prop_names
输出
打开xml / d.xml
session中的组数量= 5
#group#session $ 57
127.0.0.1 - - [17 / Aug / 2011 17:27:29]GET /view/d.xml HTTP / 1.1200 -
127.0.0.1 - - [17 / Aug / 2011 17:27:29]GET /static/ivtl.css HTTP / 1.1304 -
127.0.0.1 - - [17 / Aug / 2011 17:27: 29]GET /static/jquery.js HTTP / 1.1304 -
127.0。 0.1 - - [17 / Aug / 2011 17:27:29]GET /static/raphael-min.js HTTP / 1.1304 -
127.0.0.1 - - [17 / Aug / 2011 17:27: 29]GET /static/ivtl.css HTTP / 1.1304 -
127.0.0.1 - - [17 / Aug / 2011 17:27:29]GET /favicon.ico HTTP / 1.1404 - $
127.0.0.1 - - [17 / Aug / 2011 17:27:31]GET / viewgroup / DeviceInformation HTTP / 1.1200 -
我需要所有57个小组留下来。任何提示?
数据太大而无法序列化到会话中。现在我生成一个全局字典的密钥,并将该密钥存储在会话中。
gXmlData [path] = groups
问题在于,全球字典将会随着越来越多的密钥而永远停留,但是这个过程并不意味着长寿。
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.
However, when viewgroup
is called the session variable has reverted to the old value with only 4 members in the array!
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
Output
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 -
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屋!