[mod_python] The DOs and DONTs of mod_python

Graham Dumpleton grahamd at dscpl.com.au
Tue Oct 18 19:33:20 EDT 2005

A few additional comments on top of what Jorey has already said.

Philippe C. Martin wrote ..
> Please note that, although I must have bugs, the code I'm calling _seems_
> to 
> work flawlessly under Linux and Windows.

Besides issues with multiple interpreters, if using a multithread Apache
MPM, such as is the case for Win32 systems and "worker" MPM on UNIX, you
have to contend with multithreading. Your system may work quite happily
if you trickle requests in serially, but get requests come in at the
same time and they can trample all over each other if your module isn't
multithread safe.

There are also various issues because of mod_python's module reloading
system. Problems can also arise if you use a module which has a C code
component and it uses the simplified threading C APIs in Python.

There are a lot of gotchas.

> 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')

You shouldn't extend sys.path in Python code when mod_python is used.
The main reason is that if mod_python reloads the module, everytime that
occurs then sys.path will be extended even if the directory is already in
there. Thus, sys.path could grow and grow. This is made worse by bugs
in mod_python.publisher in versions of mod_python < 3.2.

> from SC.pilot.SC_Script_Processor import *
> def world(req):
>      ls = SC_Script_Processor() #INSTANTIATE A _BIG_ OBJECT ==> *** THIS
> IS 
>      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()

You don't set req.content_type. How is the browser mean't to know what
to do with the response. You perhaps want:

  req.content_type = 'text/plain'

Although, in 3.X the call to 'req.send_http_header()' is actually redundant.
It was only required in 2.7.X.

>      req.write("RETURN CARD ATR= " +  l_appli_conn.Get_Connection_ATR()
> )
>      return apache.OK

I have presumed that mod_python.publisher was being used because
you weren't using "handler()" for the function, but "world()". If this is the
case, you shouldn't be returning "apache.OK" as it will result in the string
"0" being appended to the response sent back to the browser.

Overall, better off perhaps saying:

  req.content_type = 'text/plain'
  return "RETURN CARD ATR= " +  l_appli_conn.Get_Connection_ATR()


More information about the Mod_python mailing list