[mod_python] the req object

Jim Gallacher jpg at jgassociates.ca
Thu Jun 14 22:28:20 EDT 2007


David Bear wrote:
> On Wed, Jun 13, 2007 at 04:15:38PM -0400, Jim Gallacher wrote:
>> Hi David,
>>
>> David Bear wrote:
>>> When is the req object made available?
>>>
>> Think of the handler method as the entry point into your code for each 
>> request.
>>
>> Consider the simplest possible handler as an example:
>>
>> AddHandler mod_python .py
>> PythonHandler helloworld
>>
>>
>> helloworld.py
>> -------------
>>
>> from mod_python import apache
>>
>> MSG = 'Hello world'
>> my_sum = 10 + 10
>>
>> def handler(req):
>>     req.content_type = 'text/plain'
>>     req.write(MSG)
>>     return apache.OK
>>
>> Note that the statement my_sum = 10 + 10 is only executed once, not 
>> every time a new request arrives. Same for MSG and our initial import 
>> statement.
> 
> OK. Great. So, my_sum = 10 + 10 is executed when apache loads and
> reads helloworld.py the first time -- and upon any subsequent module
> reloads? This makes sense to me.. is it correct?
> 
> What if helloworld.py imported another module named goodbye.py that
> had this:
> 
> goodbye.py
> -----------
> 
> newvar = 10 + 10
> 
> def coolfunc(somestring):
>    backwards = somestring.split()
>    backwards.reverse()
>    return(backwards)
> 
> If I understand, when helloworld is imported, any imports it does will
> also be executed -- and then any expressions in the imported modules
> will ALSO be evaluated when they are imported. So, newvar will be
> evaluated when helloworld is imported by apache -- but coolfunc won't
> do anything untill it is called presumably by helloword.py.
> 
> Am I approaching correctness here?

You are indeed.

Jim


More information about the Mod_python mailing list