Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Flask session member not persisting across requests

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?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...