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

Gregory (Grisha) Trubetskoy grisha at modpython.org
Tue Sep 2 13:32:27 EST 2003



On Tue, 2 Sep 2003, Guy Davis wrote:

> 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?

Yes, __builtins__ is shared between requests and using it for per-request
information is not a good idea. You can probably get away with it since
Apache 1.3 isn't multithreaded (unless you're on Windows), but it's still
a bad idea, not only for reasons of concurrency, but also __builtins__
isn't the right place to store anything.

Your best bet is to use the request object itself:

req.foo = 'bar2'

> Will the behaviour be any different under Apache 2 and mod_python 3?

Yes, on apache 2 it is going to be a lot more likely to cause trouble :-)

Grisha


More information about the Mod_python mailing list