[mod_python] PythonImport - an example?

Gregory (Grisha) Trubetskoy grisha at modpython.org
Fri Mar 21 10:36:36 EST 2003

Yes, but I would stay away from PythonImport unless you have no choice.

There's been some debate about this on the list before and it manly arises
from a misunderstanding of how things work. At first glance it seems like
a simple and obvious thing - initiate my db connections and load big data
dictionaries at server startup so that when those requests come in we're
ready to go.

But the problem is that Apache doesn't work this way. The context within
which a request runs comes into existence only after a request begins and
is a result of applying known server configuration directives (from config
and .htaccess files) to dynamic request parameters (e.g. host:  header
which dictates the virtual server to be used, and also the name of the
subinterpreter mod_python will use).

Consider also that there is no way to reliably predict ahead of time what
this request environment may look like since it is put together gradually
by handlers, which might be provided by any number of apache modules which
may choose to drastically change the request environment (e.g. rewrite a
bunch of headers). For example the virtual server and document root
information may not even be specified in the config file but is created
dynamically per-request if you're using something like mod_vhost.

But the server config file discussion during PythonImport is moot,
because, in fact, at the time when PythonImport executes (Apache child
initialization phase), Apache hasn't yet read the config file fully.

So you have to realize that PythonImport runs in this minimalistic bare
environment and needs a hint in the form of the subinterpreter name to be
useful, and sometimes getting it to work correctly can be a challenge, and
also keep in mind that using this directive isn't very portable because it
requires the interpreter name.

I personally favor not bothering with pre-initialization at all. The
downside is that the first hit to your side may take a little longer.
Another option that I've used quite successfully is to add a request to
some initialization url (using lynx or wget or whatever) into the server
start up script.


On Fri, 21 Mar 2003, Jeremy Cowgar wrote:

> Can anyone give me an example of how the PythonImport directive/code works?
> I may be mistaken, but what I want to use it for is to establish my initial db
> connections, load some classes, do some configuration, etc... Then in all my
> hander's, I should have access to that information, correct?
> Thanks,
> Jeremy
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://www.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list