[mod_python] Safety of using __builtins__ for per-request information?

Mike Looijmans mike.looijmans at asml.com
Wed Sep 3 07:21:15 EST 2003

It's not risky in this particular case, but it isn't good practice either...

Using cgi_handler will effectively make your system single-threaded,
regardless of the Apache version used. cgi_handler already shares the
"sys.stdout" and sys.stderr globals, and uses a lock to prevent two requests
to interfere. So within the request, using globals is safe.
I wonder why you use __builtins__, why not just use globals in your own
module? Just create a globals.py or so and import that from each script.

I saw performance _degrade_ because of this when I moved from 'plain' CGI to
mod_python cgihandler (apache 2.0 with worker mpm). Some long requests would
stall the server completely. My solution was to replace all "print"
statements with "output.write(...)" calls, and use the Request object for
output in mod_python scripts and sys.stdout when the server is using plain
CGI. Basically, I turned it around and made a CGI interface that emulates
mod_python (cgihandler does the opposite). The overhead for emulating CGI in
mod_python is huge, while the overhead of emulating mod_python in CGI is
neglectable compared to the interpreter load-and-start.


-----Original Message-----
From: Guy Davis <davis at guydavis.ca>
To: mod_python at modpython.org <mod_python at modpython.org>
Date: Tuesday, September 02, 2003 7:13 PM
Subject: [mod_python] Safety of using __builtins__ for per-request

>Hi all,
>I'm writing a custom handler using the mod_python.cgi_handler as a guide
>on Apache 1.3 with mod_python 2.7.  Unfortunately, our old CGI makes use
>of certain super-global variables that are not easily done away with.
>I've been able to get this to work by having the handler set the global
>vars into the __builtins__ dictionary.  Then various CGI scripts can
>pull them from __builtins__ when needed. I know using __builtins__ in
>this fashion is not recommended as it's a bit of a hack.
>My question is whether storing per-request information in __builtins__
>is risky due to concurrency reasons.   Put another way, is __builtins__
>shared between concurrent requests?  For example, person A hits a page
>and the handler stores __builtins__['foo'] = 'bar'.  At nearly the same
>time person B hits a page and the handler stores __builtins__['foo'] =
>'bar2'.  After this occurs, the CGI for person A retrieves
>__builtins__['foo'].  Will the result always be 'bar' or will 'bar2'
>sometimes be returned depending on the timing?
>Will the behaviour be any different under Apache 2 and mod_python 3?
>Thanks in advance for any responses.
>Guy Davis                 http://www.guydavis.ca
>Mod_python mailing list
>Mod_python at modpython.org

The information contained in this communication and any attachments is confidential and may be privileged, and is for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender immediately by replying to this message and destroy all copies of this message and any attachments. ASML is neither liable for the proper and complete transmission of the information contained in this communication, nor for any delay in its receipt.

More information about the Mod_python mailing list