I may be wrong, but AFAIK django is not a server - it is a framework
that runs on a web server, so every new request starts a new instance of
your site. You could use cacheing to serve static data - but this is not
like pre-loading data like one would do with zope for example.
--
regards
Kenneth Gonsalves
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.
--
regards
Kenneth Gonsalves
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
No; that is highly unlikely, unless using a threaded model to run
Django*. You would have a separate cache per interpreter process.
Ideally, you would have a hook for server_init and hook for
child_init, just like something like Apache httpd, to allow you to
load data before forking. I believe something like this is planned, or
at least desired!
Cheers
Tom
* Whether you run threaded or non threaded is entirely at the
deployer's choice. See [1] for a detailed explanation of how mod_wsgi
will run your site, depending on how you configure it. So if
deployment is not under your control, you shouldn't use methods that
rely on running a threaded model.
[1] http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading
I would disagree. If the running of your application depends upon
static content loaded from disk, it may be best to load it in
settings.py. If the code fails, your server fails to start (fail
fast).
In this particular place, it may make sense. The OP is talking about
loading static XML files, parsing them into a Python structure, and
making them available for all views, which require them to work.
As an example, say you use shared hosting, and your host writes your
DB credentials to an XML file in your webroot, readable only by the
webserver. Would it be wrong to parse that XML file in settings.py, in
order to produce the settings.DATABASES setting? Clearly not.
Django deliberately makes this path available to developers -
otherwise settings may as well come from a YAML file. It is up to you
to determine whether it is right or wrong for your particular project.
Cheers
Tom