[mod_python] multiple processing of the same python script by the handler

Graham Dumpleton graham.dumpleton at gmail.com
Sun Mar 29 21:33:46 EDT 2009

2009/3/30 David Sfiligoi <webmaster at world-vr.com>:
> Hi Graham,
> Yes I agree defining my logger outside the mod python handler was a bad idea.
> Using the req.log_error works of course.  Obviously if I wanted integrate
> this into all the other apps that revolve around those logs, i'd have to
> write a converter or change each apps which parses the log data.
> I'd like to understand this a bit better, perhaps I have the wrong basic idea
> on how the python handler works in overall, but if I install the log handler
> in the handler function and delete the log objects before ending, shouldn't
> that take care of the multiple logging issue?
> Or are you saying that each piece of code that should not produce multiple
> entries of the same thing should really be inside a module so I can let
> import_module() take care of it?

Put logging initialisation in a completely separate module outside of
document tree and not inside of the handler code files. Setup
PythonPath if need be where that separate module is located,  and then
have handler code files import that module from that other location.

This should ensure that the logging initialisation code is imported
and run only once per process. If you intentionally then change the
logging initialisation module code, you will need to restart Apache.

> Im seeing the same 'multiplicity' issue when I open a socket while in handler
> function. This is really what trigger the question.

That I can't explain at this point. I was assuming that your
duplicated logging may have been confusing the issue.

> Thanks,
> David
> On Sunday 29 March 2009 05:45:01 am Graham Dumpleton wrote:
>> If you change a mod_python handler script, it will be automatically
>> reloaded. This would be a problem in your code as you installed a log
>> handler each time it is loaded into the same process. Having multiple
>> instances of the log handler installed would result in the same
>> information possibly being logged multiple times.
>> I would suggest you add inside your handler script:
>>   req.log_error("I have been called")
>> If that appears twice in the Apache error log file then I would be
>> concerned, otherwise I would say the problem is with your use of the
>> logging module.
>> Also read about import_module() in:
>>   http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html
>> as it explains a bit about module reloading and the way to preserve
>> global data or avoid stuff which performs additive operations each
>> time it is loaded.
>> Graham

More information about the Mod_python mailing list