On 07/05/06 03:34, Iain Duncan wrote:
> I stumbled on some behaviour that I'm sure is explained somewhere but
> not in the tutorials so I haven't found it.
> 
> I made a global log object for debugging. I noticed instantiating
> objects in the view global name space executes before anything else in
> the views module on page load, very useful for global containers or
> loggers. I also noticed that on subsequent page loads, this code does
> NOT get re-executed, and the state of member variables in the global
> object are preserved.
> 
> Questions: how is this accomplished? Are global objects serialized
> behind the scenes in a session indexed table, and if so which one?
> Is this behaviour safe to *rely* on for preserving state in an ongoing
> session? IE could you safely implement a shopping cart that way? Seems
> like very useful default behaviour.
> 

Code in the global namespace is executed at module import time.
These variables live as long as the python interpreter runs. (at least 
thats my understanding of it)

This is great for constants and such, but you can't rely on it to store 
state variables as, depending on the environment/server you run python 
on, you have no or little control of how or when the interpreter is 
restarted.

With mod_python on *NIX for example, you have multiple apache processes 
each with it's own python interpreter. An apache process serves n 
requests, then it's killed and a new one is started. Subsequent requests 
from one client will not necessarily be served by the same apache process.

So no, it's not safe to store important data there. It's better to use a 
session, database, or file based solution.

> Suggestion, maybe there should be a mention of this in the views
> tutorial? Or was there one and I've been cramming this all in to my head
> so fast it bled out the other end? ;)
> 
> Loving it all so far!
> Iain
> ( And my tendons are sooo happy not to be reaching for [EMAIL PROTECTED]> all 
> the time! )
> 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to