Keith Devens .com |
Saturday, August 30, 2008 | ![]() |
| The direct pursuit of happiness is a recipe for an unhappy life. – Donald Campbell | ||
|
| ← It's like beating your head against a wall... | The Onion: Mom Finds Out About Blog → |

Simon Willison (http://simon.incutio.com/) wrote:
Keith (http://keithdevens.com/) wrote:
Simon, any ideas on how to make sure your global environment is clean so you're not carrying around baggage in memory? Or, could you tell me how to make sure that errors go to the browser instead of the Apache error log? Do you know why they couldn't overload "print" to make it print to the request instead of having to call req.write() all the time?
Sorry for all the questions... thanks 
Simon Willison (http://simon.incutio.com/) wrote:
You can overload print yourself - just use "sys.stdout = req" at the start of your handler. As for the global environment, as long as you don't define any global variables you'll be fine and in Python globals are only created if you assign to something outside of a function or class. Actually this is pretty useful because it means you can do things like compiling regular expression objects just once for each Apache child.
Apache children are important to think about as well. mod_python actually creates a complete environment for every Apache child spun off by the Apache server. These children can be configured to have a lifetime of usually something like 5 minutes, after which the'll be killed and restarted (I forget what the default setting for this is). This means that any cunning plans involving caching stuff in global variables between requests will have to take in to account the fact that you'll have one cache per apache child, which can add up to a lot of memory if you aren't careful.
I'm not sure why your errors are going to the apache log - I've used PythonDebug on and had the errors appear in the browser window instead.
Keith (http://keithdevens.com/) wrote:
I'm not sure why your errors are going to the apache log
I guess PythonDebug has to be specified relative to a particular directory. I just had it in the global Apache config and it wasn't working, but if I'd had it in a <Directory> block it probably would have worked. I stuck it in my .htaccess and it worked.
Apache children are important to think about as well
Yikes, thanks. I thought it was only Python sub-interpreters I had to worry about.
Jason wrote:
Good info thanks
Feel free to post a comment below. Please see my comment policy.
Formatting Rules (No HTML):
Generated in about 0.157s.
(Used 8 db queries)

I share your concerns about the portability of mod_python code to other environments, although our experience with mod_python here at work has been pretty reassuring so far in terms of reliability. That's why one of my main hopes for the Python Web-SIG is that it will help define a simple standard API which applications can be written against that will allow easy portability between mod_python, Twisted, CGI and so on. If you're interested in that kind of thing you should sign up for the mailing list.