[mod_python] Win32 threading issue

Chris Trengove trengove at econdata.com.au
Sun May 6 15:07:53 EST 2001


I have been testing the following script on Apache with mod_python (Apache
1.13.17, mod_python 2.7.2, Windows 2000). I have been interested in testing
the possible throughput of this (and more useful scripts), so I have
another Python script which continually loads this "page".

from mod_python import apache
import thread

def handler(req):
    req.content_type = "text/plain"
    req.send_http_header()
    req.write("%d\n" % thread.get_ident())
    return apache.OK

When I run more than one copy of the driver script (in multiple consoles),
I get behavior indicative of some threading problems. Specifically, some
connections get "lost", the Apache error log reports various instances of
"IOError: Write failed...", and I occassionally get an access violation
inside Apache. Similarly, if I only run one copy of the driver script, and
just keep hitting the refresh button in a browser, the browser
occassionally returns TWO numbers, and sometimes reports an error in
retrieving the data. Note that I have tested my driver program retrieving a
simple text file, and everything works as expected, so it does not appear
to be a problem with Apache. (The problem also goes away if I set
ThreadPerChild to 1 in the Apache configuration.)

I have also found that, if I comment out the req.write() statement in the
handler, then there are no problems. From my examination of the
implementation of the request object, I see that req_write() contains calls
to Py_BEGIN_ALLOW_THREADS / Py_END_ALLOW_THREADS, whereas
req_send_http_header() contains no such calls. This suggests to me that
some thread switching is occurring inside req_write() which is messing with
the state of the interpreter.

I would be most grateful if anyone has any information to offer on this.

Chris Trengove




More information about the Mod_python mailing list