[mod_python] Multiple loading of same module with winnt MPM

Michael Sanders m.r.sanders at gmail.com
Thu May 10 06:26:44 EDT 2007


Graham,

Here is the test PSP file. In my test, this is loaded twice 
(simultaneously) with a newly re-started Apache by an HTML file 
containing two Javascript 'XMLHttpRequest()' calls. (I can post the HTML 
if required).

Note: replacing the '~' in the path passed to apache.import_module with 
the full path gives no change in behaviour.

---- test.psp -------------------------
<%
#import os
#import sys
#ourpath = os.path.dirname(psp.req.canonical_filename)
#sys.path.append(ourpath)
#import mymod

mymod = apache.import_module("~/mymod.py")
mymod.logger.warning('test')

%>
test
---------------------------------------


The imported module. Note: a random number is generated when the module 
is imported and this number is appended to all logged messages, so as to 
identify the logged messages with instance of the imported module (e.g. 
see 'mylog.log' below).

---- mymod.py -------------------------
import sys
import logging
import random
rnd = random.random()
log_file = "c:/temp/mylog.log"
logger = logging.getLogger()

log_handler = logging.FileHandler(log_file,"a")
log_handler.setFormatter(logging.Formatter(fmt='%(asctime)s %(message)s 
'+str(rnd)))
logger.addHandler(log_handler)
logger.warning("added a new log handler")
---------------------------------------


The Apache .htaccess file. The only mod_python related directive in 
httpd.conf is the 'LoadModule' line. There is no PythonPath set in 
httpd.conf and no system PYTHONPATH environment variable set.

---- .htaccess file -------------------
AddHandler mod_python .psp
PythonHandler mod_python.psp
PythonDebug on

<FilesMatch "\.py">
     Order allow,deny
     Deny from all
</FilesMatch>
<FilesMatch "$\.ht">
     Order allow,deny
     Deny from all
</FilesMatch>
---------------------------------------


The Apache access log:

---- access.log -----------------------
10.5.9.120 - - [10/May/2007:10:43:12 +0100] "GET /webapp/test.html 
HTTP/1.1" 304 -
10.5.9.120 - - [10/May/2007:10:43:12 +0100] "GET 
/webapp/test.psp?sid=0.3172181574260018 HTTP/1.1" 200 8
10.5.9.120 - - [10/May/2007:10:43:12 +0100] "GET 
/webapp/test.psp?sid=0.05203795490300955 HTTP/1.1" 200 8
---------------------------------------


The Apache error log:

---- error.log ------------------------
[Thu May 10 10:43:12 2007] [notice] mod_python (pid=5632, 
interpreter='DELL14.mydomain.local'): Importing module 'C:\\Program 
Files\\Apache Software Foundation\\Apache2.2\\htdocs\\webapp\\mymod.py'
[Thu May 10 10:43:12 2007] [notice] mod_python (pid=5632, 
interpreter='DELL14.mydomain.local'): Reimporting module 'C:\\Program 
Files\\Apache Software Foundation\\Apache2.2\\htdocs\\webapp\\mymod.py'
---------------------------------------


The log created by test.psp. This shows two logging handlers being 
created - one when the module is first imported, and the second when the 
module is re-imported. The 'added a new log handler' and test' messages 
generated by the second call to 'test.psp' are therefore logged twice, 
once by each handler.

---- mylog.log ------------------------
2007-05-10 10:43:12,404 added a new log handler 0.884955893555
2007-05-10 10:43:12,404 test 0.884955893555
2007-05-10 10:43:12,420 added a new log handler 0.884955893555
2007-05-10 10:43:12,420 added a new log handler 0.644262952239
2007-05-10 10:43:12,420 test 0.884955893555
2007-05-10 10:43:12,420 test 0.644262952239
---------------------------------------


Changing 'test.psp' to use the standard Python module importer (by 
uncommenting the appropriate lines and commenting out the line 
containing 'apache.import_module'), the following logs are generated:

---- mylog.log ------------------------
2007-05-10 10:55:10,733 added a new log handler 0.851457438205
2007-05-10 10:55:10,733 test 0.851457438205
2007-05-10 10:55:10,733 test 0.851457438205
---------------------------------------

---- access.log -----------------------
10.5.9.120 - - [10/May/2007:10:55:10 +0100] "GET /webapp/test.html 
HTTP/1.1" 304 -
10.5.9.120 - - [10/May/2007:10:55:10 +0100] "GET 
/webapp/test.psp?sid=0.5035066189141854 HTTP/1.1" 200 8
10.5.9.120 - - [10/May/2007:10:55:10 +0100] "GET 
/webapp/test.psp?sid=0.239310630890464 HTTP/1.1" 200 8
---------------------------------------


I forgot to mention in my first mail, that I'm using Python 2.5.

Thanks again for your help.
Michael

Graham Dumpleton wrote:
> Post the snippet of PSP code you are doing the import from.
> 
> Post the Apache configuration snippet you are using so it can be seen
> whether you are setting PythonPath to include a directory which
> overlaps the document area and whether you are using Apache style
> forward slashes in paths or Windows backwards slashes.
> 
> Post the snippets of the log files which show when the module is being
> loaded so can see the paths it is being loaded under. Especially post
> any warnings output about a path appearing in sys.path as well as
> mod_python module importer path.
> 
> The only issue I know of with PSP and the new module importer is that
> if you use 'import' rather than 'apache.import_module()', the 'import'
> will always use standard Python module importer and not
> 'apache.import_module()'. Thus if you were trying to import a module
> from the same directory it by default will not work. If you fiddled
> PythonPath to include that directory, thus creating an overlap in the
> two systems, it will import but thus may be imported separately by
> both mechanisms.
> 
> To address this latter problem, PSP needs to be changed to fake up
> some information in the page pseudo module so that it thinks it was
> imported used mod_python importer in the first place and will thus map
> 'import' through to 'apache.import_module()' properly.
> 
> Graham
> 
> On 09/05/07, Michael Sanders <m.r.sanders at gmail.com> wrote:
>> Environment:
>> - mod_python 3.3.1
>> - Apache 2.2.3 running on Windows XP
>> - PSP request handler
>>
>> [Issue 4] described at the following link still appears to be present in
>> mod_python 3.3.1:
>> http://www.dscpl.com.au/wiki/ModPython/Articles/ModuleImportingIsBroken#line-50 
>>
>>
>> I have a PSP file that imports a module using 'apache.import_module'. If
>> I re-start Apache and then fire two simultaneous requests at the PSP
>> file, mod_python loads the module twice. (The module in question when
>> imported  sets up a logging handler. The initial entry written by this
>> module to the log file is duplicated and subsequent calls to this
>> handler also give duplicate entries in the log file).
>>
>> If I import the module using the standard Python 'import', this problem
>> does not occur. Likewise, if I use the 'vampire.importModule' method,
>> the problem does not occur. The issue only happens when using
>> 'apache.import_module'.
>>
>> Has anyone else experienced this? Is there a fix?
>>
>> Many thanks,
>> Michael Sanders
>> _______________________________________________
>> Mod_python mailing list
>> Mod_python at modpython.org
>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
> 



More information about the Mod_python mailing list