[mod_python] Is it possible to not reuse interpreters?

Graham Dumpleton grahamd at dscpl.com.au
Mon Jun 13 00:49:08 EDT 2005

On 13/06/2005, at 12:10 PM, Bart wrote:

> On 6/12/05, Graham Dumpleton <grahamd at dscpl.com.au> wrote:
>> On 13/06/2005, at 11:14 AM, Bart wrote:
>>> Hey there,
>>> Apologies if this has been asked and answered before,
>>> but is there any way to make apache/mod_python
>>> temporarily *not* reuse the same interpreter?
>> No.
>>> Google wasn't too helpful on the matter other
>>> than providing the reasons.
>>> While I'm clear on why this is generally good, while
>>> coding it's quite annoying that I have to restart
>>> apache just to not get it to randomly fail on code
>>> that's in reality any your-guess-is-as-good-as-mine
>>> old version, but practically guaranteed not to be
>>> the current one.
>> What you want is automatic module reloading.
>> Have a look at the automatic module reloading mechanism implicit
>> within the apache.import_module() function. See documentation at:
>>    http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html
>> This same method is used by mod_python to load a handler. In that
>> case auto reloading is controlled by the PythonAutoReload directive.
>>    http://www.modpython.org/live/current/doc-html/dir-other-par.html
>> The current auto reloading mechanism has a number of shortcomings
>> and will not always work, but depending on the complexity of your
>> code it may be sufficient.
> 'Implicit' meaning I can use import normally rather than calling
> apache.import_module, ie. that it should just be working?
> And if the two are the same mechanism, doesn't that the
> 'is there newer code' check is what's failing?

No, the "import" statement is distinct and still is the normal Python
module importing system. The words "implicit within" were a
bad choice, read it as "implemented by". Because they aren't the
same anything imported using "import" isn't in anyway automatically

Even if you replaced imports at global scope within a handler module
with apache.import_module(), it still may not do what you want as the
code which checks for changes will only detect a change in the handler
module and not any module which is imported by it using

This is because the check for the sub import is only done at the time
of the call which is when the top level module is imported. Thus, only
way to get it all imported correctly is to touch the top level handler
to update the modification time. That way it will import the top level
and because the sub import has changed it will then reimport it as well.

One way of getting it to work a bit better is to sub import the module 
apache.import_module() within the handler function itself each time
it is called. That way the check of whether the sub import code has
changed is on each request and any changes will be updated 

> I haven't disabled the PythonAutoReload thing (I'll enable it
> explicitly in my config to be sure), so it should be working, right?
> Are you telling me that (given it's not that) when it fails
> this problem can't be solved through apache or mod_python at all?

As I said, there are shortcomings in the apache.import_module() as it
is currently implemented.

> Is there any predictability to the method that I could affect?
> Removing pyc's, touching everything, sending a signal to kill
> running python interpreters in apache, something like that?

Some people use the method of touching everything, but you will need
to use apache.import_module() instead of the "import" statement for
those parts of your web application you want to be capable of being
reloaded. You should avoid using apache.import_module() on modules
installed in site-packages though.

> Actually, restarting apache is acceptable for me, but it
> appears to me there just has to be a better way, because
> presumably people develop this way...

It is certainly possible to get it to work properly, just not with
mod_python at the moment. Maybe in the future sometime it will be fixed.

If restarts are an acceptable way of managing this at the moment, I
would suggest you go with that.


More information about the Mod_python mailing list