[web2py] Manage session.VARS best practices

488 views
Skip to first unread message

Richard

unread,
Nov 3, 2015, 1:43:47 PM11/3/15
to web2py-users
Hello,

Let say I put some data in session.var1 for reuse in case user want to download the content of the page with the help of buttons for the purpose which call the same function with one more prameter telling the controller function which file format the user expect...

So you can check if file format parameter is there and pass the session.var1 to the sub funciton :

if request.vars.file_format:
    export_to_file(session.var1)
else:
    del(session.var1)  # Here I can't delete var1 from session in case a new call is made to the controller for the first time...

But, what to do with session.var1 in case the which will remain there if I load any other app pages??

Should I create a clean_up_session()?

Something like this :

def clean_up_session():
    if session.var1 and request.function != 'my_controller_func' and not request.vars.file_format:
        del(session.var1)

Which I would need to executed at every request (can't be put in model file for that purpose)...

But I am curious what is consider the best practices regarding session variables ?

Thanks

Richard

Anthony

unread,
Nov 3, 2015, 1:59:48 PM11/3/15
to web2py-users
I'm not sure there is any need to clean up individual sessions unless you are storing a large amount of data. On the other hand, you might want to clean up expired session files once in a while -- there is a script for that.

Anthony

Richard Vézina

unread,
Nov 3, 2015, 2:04:21 PM11/3/15
to web2py-users
There is large amount of that data that why I have this concern...

Is cleaning the sessions/* files delete data put in session?

Richard

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Nov 3, 2015, 2:19:42 PM11/3/15
to web2py-users
On Tuesday, November 3, 2015 at 2:04:21 PM UTC-5, Richard wrote:
There is large amount of that data that why I have this concern...

You might want to do some testing (i.e., checking response times and RAM usage) before bothering to manage session keys. Note, the session data for a given user is only loaded into RAM during the period of a given request (in between requests, it resides in the session file).

If there is a large portion of the session that can safely be deleted at some point, feel free to do something like what you have suggested and just delete the relevant keys. One thing to keep in mind is that if the session doesn't change during a given request, web2py does not bother to write out to the session file, so if you do delete some keys, you will be prompted a file write during that request. The overhead of the extra file writes must therefore be balanced against the overhead of keeping the extra data in the session instead.
 

Is cleaning the sessions/* files delete data put in session?

When you clean up session files, you are typically deleting files of sessions that have already expired (based on the Auth login expiration) and/or are presumed no longer to be active (based on the time since last modified). The entire file is deleted, so of course you lose all the data.

Anthony

Richard Vézina

unread,
Nov 3, 2015, 2:28:08 PM11/3/15
to web2py-users
Thank you Anthony, I appreciate you input... I was concern about exactly that, I mean keep clean up the session each request has a cost... I will need to make a check, it would be logic that if the session keys to be are not present in session that the only overhead should be the clean up funciton call and the session keys check...

Thanks again

Richard

--

Anthony

unread,
Nov 3, 2015, 2:36:02 PM11/3/15
to web2py-users
Yes, simply making the check for the session keys to see if cleanup is needed should add minimal overhead. Likewise, if the deleting itself is infrequent (or if it tends to occur when you have other session changes that need to be written to the file anyway), then there shouldn't be much additional impact of writing the updated session. On the other hand, if you're constantly deleting and re-adding the same session keys request after request, it might not be worth it.

Anthony


On Tuesday, November 3, 2015 at 2:28:08 PM UTC-5, Richard wrote:
Thank you Anthony, I appreciate you input... I was concern about exactly that, I mean keep clean up the session each request has a cost... I will need to make a check, it would be logic that if the session keys to be are not present in session that the only overhead should be the clean up funciton call and the session keys check...

Thanks again

Richard
On Tue, Nov 3, 2015 at 2:19 PM, Anthony wrote:
On Tuesday, November 3, 2015 at 2:04:21 PM UTC-5, Richard wrote:
There is large amount of that data that why I have this concern...

You might want to do some testing (i.e., checking response times and RAM usage) before bothering to manage session keys. Note, the session data for a given user is only loaded into RAM during the period of a given request (in between requests, it resides in the session file).

If there is a large portion of the session that can safely be deleted at some point, feel free to do something like what you have suggested and just delete the relevant keys. One thing to keep in mind is that if the session doesn't change during a given request, web2py does not bother to write out to the session file, so if you do delete some keys, you will be prompted a file write during that request. The overhead of the extra file writes must therefore be balanced against the overhead of keeping the extra data in the session instead.
 

Is cleaning the sessions/* files delete data put in session?

When you clean up session files, you are typically deleting files of sessions that have already expired (based on the Auth login expiration) and/or are presumed no longer to be active (based on the time since last modified). The entire file is deleted, so of course you lose all the data.

Anthony

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.

Richard Vézina

unread,
Nov 3, 2015, 4:30:33 PM11/3/15
to web2py-users
The data are not the same or I can make this assomption since they can change each time the controller function it calls... The controller take parameters into vars and make db query and render a resulting table... There is numerous differents parameters and the requested data are by nature changing or say differently the interess about these data shift over new available data...

Am I right in my understanding that session.keys get write into session file only once when it get define for a given request and in the next request if the session.key1 is still there (unchanged) it won't be re-write to session file? At start I would just avoid that the session populated data were not transported all around the app on each request for nothing... If they get write once when they get created/updated and that there is no overhead at having it there, I can leave with them, though if they stays in memory (ram) it can start to be an issue if many user start to generate these reports...

Thanks

Richard

To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

Anthony

unread,
Nov 3, 2015, 4:41:41 PM11/3/15
to web2py-users
Am I right in my understanding that session.keys get write into session file only once when it get define for a given request and in the next request if the session.key1 is still there (unchanged) it won't be re-write to session file?

Writes to the session file are not granular -- it's all or nothing. If the session changes at all during a request, then the existing session file is completely overwritten with the updated version of the session. However, as long as the session doesn't change at all during a request, then no write is performed. If you have a large session and delete a single key, you will prompt a write of the entire remaining session to the file.

Anthony

Richard Vézina

unread,
Nov 3, 2015, 5:12:32 PM11/3/15
to web2py-users
Thank you for clarification I really appreciate...

Also about previous posted kind of pseudo code above this work better in case var1 is not yet defined :

elif 'var1' in session:
        del(session.var1)

--

Anthony

unread,
Nov 3, 2015, 5:30:46 PM11/3/15
to web2py-users
On Tuesday, November 3, 2015 at 5:12:32 PM UTC-5, Richard wrote:
Thank you for clarification I really appreciate...

Also about previous posted kind of pseudo code above this work better in case var1 is not yet defined :

elif 'var1' in session:
        del(session.var1)

Sure, or just:

session.pop('var1', None)

Anthony
Reply all
Reply to author
Forward
0 new messages