[mod_python] The DOs and DONTs of mod_python

Jim Gallacher jg.lists at sympatico.ca
Tue Oct 18 19:27:39 EDT 2005

Jorey Bump wrote:
> Philippe C. Martin wrote:
>> Since I just managed to seg fault apache I feel I should explain what 
>> my goal is with mod_python.
>> I already have a fairly big library written in python which purpose is 
>> to discuss with smart cards but also provide "software" enryption 
>> capabilites: As such, this library uses modules that in turn talk to C 
>> code.
>> The ultimate goal is to have users need to authenticate themselves to 
>> the servers (on smart card on each side) in order to obtain access to 
>> certain pages.
>> I must also say that there is a failry large data set that needs to be 
>> loaded in memory.
> Why?
>> The code below shows the line that creates the core dump: it 
>> instantiates that _large_ object and does a lot in the process.
>> As I am an application developper more than a web guy, there are 
>> perhaps some obvious "NO NOs'" I should be aware of that are related 
>> to the apache environment.
>> Please note that, although I must have bugs, the code I'm calling 
>> _seems_ to work flawlessly under Linux and Windows.
> It might, for a single interpreter. But apache might be forking multiple 
> children on your system. Can your machine handle 5 or more copies of 
> this object in memory?
>> from mod_python import apache
>> import os
>> import sys
>> if os.name == 'nt':
>>      sys.path.append("Z:\\dev")
>> else:
>>      sys.path.append('/home/philippe/dev')
>> from SC.pilot.SC_Script_Processor import *
> This may happen multiple times. It will be cached in a prefork 
> environment, but once for each child. I have no idea how it works on 
> Windows.
>> def world(req):
>>      ls = SC_Script_Processor() #INSTANTIATE A _BIG_ OBJECT ==> *** 
> How big? If it's a data set, why not use a database?
>>      ll = ls.Reader_List() # get the reader list
>>      #Grab ATR from card - assume first reader
>>      l_appli_conn = ls.Get_Connection(r1)
>>           req.send_http_header()
>>      req.write("RETURN CARD ATR= " +  l_appli_conn.Get_Connection_ATR() )
>>      return apache.OK
> We need to see your code in the imported module. It's a black box to us.

Also, make sure that you consider things like permissions, paths and 
environment variables. These will be different in mod_python vs the 
command line. The mod_python interpreter will run with same uid and gid 
as apache, and your path and environment will be much simpler. Try 
adding req.write('%s' % os.environ) and compare that with os.environ 
that you see in the python interpreter run from the command line.


More information about the Mod_python mailing list