4

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?

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You could use redis backend to store the dictionary. There is a nice functionality to store data in a key with expiry date (redis.io/commands/setex). So on first set you define expiry with kind of session timeout. Than on each get you either prolong expiry or return session expired (redis.io/commands/expire). Value data size limit is pretty big (stackoverflow.com/questions/5606106/…).
0

Maybe your data is too big.

If your data is larger than 4KB, a server-side session is needed. Have a look at Flask-Session.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.