On Thu, Apr 5, 2012 at 1:31 PM, kenneth gonsalves
<law...@thenilgiris.com> wrote:
> On Thu, 2012-04-05 at 05:25 -0700, Lars Ruoff wrote:
>> the question is not about serving static data (like HTML, images
>> etc.), but having global python variables initialized once, for all
>> instances of the site, that is, so they can be used from within each
>> view.
>
> this is precisely the point I addressed in my reply. Every request to
> django starts a new instance of django - and you have to initialise the
> variables again - cacheing may improve the performance. Django is not a
> server.

This is not true, please do not repeat it. Of course there are things
that are setup and torn down as part of the request/response cycle,
but there are many parts that are not.

OP: I have a couple of solutions. One of the first things the server
does is import your settings, so if you load and parse your static
data at that point, then you will have access to it from that point
onwards.

A second solution, somewhat less hacky, but not actually achieving
what you wish, is to have a function which loads, parses and returns
the data, and memoize the result. Further calls to the function,
within the same request or not, will not require the data to be loaded
again.

I use the second approach to handle static XML files that must be
parsed and loaded. One benefit of the second approach is that you can
memoize based upon a test. I use this so that as soon as the file on
disk has changed, the contents will be reloaded the next time the
function is called. The data is both static and dynamic!

Cheers

Tom

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to