[mod_python] CGI handler and multithreading: Can i have multiplesys.stdout objects?

Mike Looijmans mike.looijmans at asml.com
Fri Feb 28 08:38:54 EST 2003

>Your best bet is the prefork MPM. Remember that the 10M per child is
>actually a lot less because pages for executables are shared between
>processes on (most) UNIXes, certainly on Solaris they are. I find it hard
>to believe that it is slower than regular CGI - it should be at least
>several times faster, though not nearly as efficient as using the
>publisher or a native handler.

The trouble with performance is in our clients' access patterns. Basically, at
8:00 the whole world comes falling dowon on the machine and everybody wants to
use it, accessing it many times each second. The rest of the day it's rather
quiet. What appears to happen is that the apache child processes get their
data segments swapped out during the quiet day to make room for other stuff
that is running on the same machine. Then in the morning, that must all be
swapped back and the first requests are incredibly slow. I noticed that the
'startup' effect almost vanished when i migrated from apache 1.3 to 2.0
(worker mpm). I think that's because the worker system can keep 20 child
threads ready for action whereas the preforked processes tend to fall into
coma. Also, the CGI scripts are run in a separate process by the server, which
seems to reduce the overhead compared to the prefork way of starting the CGI.
Apache 2.0 with prefork behaved similar to that.

>The ultimate solution is not use CGI of course :-)

That is the intention, ultimately...

I think I have to dive into the code and start working. Probably, the best way
for me to set things up is to modify all the scripts to write to a "file like"
object and get their data from a FieldStorage instance. So they'd all have a
'main' like:

handlercode(output, form, database):
    output.write('<P>Hello world</P>')

And then call it from either a CGI wrapper:

    module = __import__(scriptname)
    module.handlercode(sys.stdout, cgi.FieldStorage(), db_connect())

Or cal it from a mod python handler:

    module = __import__(scriptname)
    module.handlercode(req, utils.FieldStorage(), recycle_db_connection())

And weed out all the CGI environment calls (e.g. SERVER_NAME) and replace them
with some object to handle that transparently too.

More information about the Mod_python mailing list