[mod_python] How can I turn off apparent caching of python code?

Graham Dumpleton grahamd at dscpl.com.au
Sun Aug 29 20:44:11 EDT 2004

On 26/08/2004, at 6:28 AM, Jorey Bump wrote:

> Tobiah wrote:
>>> Within your application you can do this:
>>>  req.headers_out['Pragma'] = 'no-cache'
>>>  req.headers_out['Cache-Control'] = 'no-cache'
>>>  req.headers_out['Expires'] = '-1'
>>> Or you can simply set the headers in httpd.conf or .htaccess:
>>>  <Directory /var/www/foo/site>
>>>   AddHandler python-program .py
>>>   PythonHandler mod_python.publisher
>>>   PythonDebug On
>>>   # set headers to prevent caching for anything in this directory
>>>   Header set Pragma "no-cache"
>>>   Header set Cache-Control "no-cache"
>>>   Header set Expires "-1"
>>> </Directory>
>> My understanding of the entire request process is limited,
>> but don't those directives only tell the client (browser)
>> not to cache content?  My goal is to get mod_python to reload
>> imported modules when their source file date is newer then
>> the last import time.
> Yes, this is only related to Graham's point about cached pages.

The only problem with relying on setting these in the latter way is 
you don't have access to httpd.conf. One can put them in the .htaccess
file, but only if mod_headers is loaded as a module into Apache in the
first place. If it isn't loaded, you are back to the problem of not
being able to modify httpd.conf to even add the loading of mod_headers
in the first place. :-(

Also, I don't want to specify that all resources have cache control
disabled. This is because in Vampire, the mod_python handler I have been
developing, it is possible to mix mod_python handlers and normal files
in the same directory. For normal files, which have static content, I
want normal caching arrangements to apply.

Thus, what I actually would recommend is using:

   <Files *>
   ErrorHeader set Pragma "no-cache"
   ErrorHeader set Cache-Control "no-cache"
   ErrorHeader set Expires "-1"

That is, only explicitly turn off caching for error responses. For 
responses, then have each mod_python handler use:

   req.headers_out['Pragma'] = 'no-cache'
   req.headers_out['Cache-Control'] = 'no-cache'
   req.headers_out['Expires'] = '-1'

This gives you specific control although with some extra coding in every

The reason for the ErrorHeader statements, as alluded to in my original
email, is to stop proxies caching an error response and not actually
making the request to the web server again. I would have though that an
error response should never be cached, but a proxy that I have to go
through at my work does exactly that. I would have to wait some period
of time for it to throw the cached error page away before I could then
pickup up the response from my now modified and correctly functioning
mod_python handler.

Graham Dumpleton (grahamd at dscpl.com.au)

More information about the Mod_python mailing list