[mod_python] Subinterpreters and index.py

Nicolas Lehuen nicolas at lehuen.com
Fri Jul 2 19:57:29 EDT 2004


Grisha wrote:
> On Fri, 2 Jul 2004, Nicolas Lehuen wrote:
> 
> > This could be fixed in mod_python.publisher if the module_name was 
> > built using the path part, not only the file part (line 67 of 
> > publisher.py)
> 
> But if someone wanted to use path_info in their application, 
> this wouldn't work.
> 
> > and by loading the module by directly specifying the file 
> to load and 
> > not adding the path of the file to the call to 
> import_module (line 98).
> 
> It's not so much how you load the file as the name you give 
> the module. 
> Right now it is the file name, I suppose it could be made the 
> full path of the file, though there may be some kind of a 
> gotcha with this approach.

Well, you would need to encode the path in a way that the result is a valid
module name, for example by replacing '/' and '\' by '.', spaces by
underscores and so on. The "so on" can be a bit tricky depending on the OS
(for special characters valid in filenames but not in module names like '.'
or maybe '~') and locale (for accentuated characters) but a simple
re.sub(r'[/\\]','.',re.sub(r'[^a-zA-Z0-9\./\\]','_',file)) can help for a
start.

The funny thing is that apache.import_module already gets the filename from
the module by having a look at its __file__ attribute (line 422 of
apache.py), so there IS a relation between physical files and modules.
import_module only need to make sure that the inverse is true, e.g. by
naming modules according to the full filename (including path).

There may be benefits if modules from subdirs where created within the
module corresponding to the subdir itself, i.e. if mod_python could create
packages corresponding to directories as well as modules corresponding to
files. I don't know. But the beauty of Python and mod_python is that nothing
prevents me or anybody else to give it a try (hence mod_python Servlets).
mod_python gives us access to the internal of Apache, and Python inherently
gives us access to its internals. So if we cannot do precisely what we want
with them, when else can we ?

> Also - have you looked at PythonInterpPerDrectory directive? 
> Could this be the solution to what you're looking for?
> Grisha

Yeah I've seen it, but this forces me to have all my scripts in the same
directory in order to share data between them. I cannot use subdirectories
in an application if I want to share data with the rest of the application.
For some of my applications, this is a problem, since I like them neatly
packaged.

I'm truly willing to share data between scripts, because that's why I chose
mod_python in the first place. I'm running it under Apache2 with a threading
model, and I wrote a small DBI connection pool module, so I really share my
database connections. Likewise, I have a few objects that are quite costly
to initialize (ternary search trees built from database for speed and search
flexibility), and there really is a benefit to sharing those objects between
all serving threads.

Regards (and kudos for mod_python !),

Nicolas Lehuen



More information about the Mod_python mailing list