[mod_python] Servlet Loading problem 1 year later

Daniel J. Popowich dpopowich at comcast.net
Mon May 22 08:30:04 EDT 2006

Manera, Villiam writes:
> This problem happened rarely, but now it raises many times a day.
> I modified the servlet.py in order to do some debugging and it seems
> that  the statement:
> if not issubclass(klass, Servlet): sometimes works and sometimes not.

Here's a modification of an explanation I gave someone else last June

If modifying servlet.py and PythonAutoReload is "on" then servlet.py
is being reloaded by mod_python which will create a new Servlet class
in the python interpreter.  Servlets instantiated *after* the change
will not have a problem, but cached instances created *before* the
modifications to servlet.py will be referencing the wrong class (the
reloaded Servlet is not the same class as the one when the cached
objects were instantiated).  This can create quite unpredictable
results when using the prefork MPM because different processes may or
may not have had a cached instance, so sometimes it works and
sometimes it doesn't depending on which process handles your request.
Restarting apache fixes your problem: you get fresh copies of
everything in new python interpreters.

To avoid this:

  1. You really shouldn't change servlet.py.  Put
     modifications/extensions in subclasses...the power of OO and

  2. In development mode, I configure my prefork apache with:

        MaxRequestsPerChild  1

     Sure, it's a performance hit, but you NEVER have to deal with
     stale python module importing issues and you don't have to
     litter your code with special-case importing code, etc.


Daniel Popowich

More information about the Mod_python mailing list