[mod_python] Several handlers

Graham Dumpleton grahamd at dscpl.com.au
Sat Jan 13 21:37:55 EST 2007


On 14/01/2007, at 2:34 AM, export at hope.cz wrote:

> Graham,
> Thank you for your help.
> Now Apache can find my inputinput filter.
> Here it is:
> ################
> from mod_python import apache
> import os, shutil
>
> def log(text):
>     f = open('C:/Python23/Lib/site-packages/django/core/handlers/ 
> log', 'a')
>     f.write(text)
>     f.write('\n')
>     f.close()
>
> def inputfilter(filter):
>     if filter.req.method != 'POST':
>         filter.disable()

Normally 'filter.pass_on()' would be used here, not 'filter.disable()'.

>         return
>     f=open('C:/Python23/Lib/site-packages/django/core/handlers/ 
> filter.log','ab')
>     log('first read')

You would be better of using Apache's own logging mechanism instead
of your own. Thus instead use:

   filter.req.log_error('first read')

The messages will then appear in the Apache error log whatever that  
may be
on Windows.

>     s = filter.read()
>     while s:
>         log('writing (%s)' % len(s))
>         f.write(s)
>         f.flush()
>         filter.write(s)
>         log('loop read')
>         filter.flush()

It is not necessary to explicitly flush the filter. Leave this out  
until you have
a reason for really doing it.

>         s = filter.read()
>     if s is None:
>         log('closing')
>         filter.close()
>         raise "error"

Huh!!!!  Your raising an exception here. Do you really mean to do  
that? This
would always result in the client getting back an error. The  
following 'f.close()'
would never be called, so you would be at the mercy of the garbage  
collector
as far as the file being closed. If for some reason that isn't being  
done straight
away it could cause resources to run out.

>       f.close()
> ################
> But I would expect that it will read some data, write to a file and  
> release the memory and
> again.
> But it does NOT. As it reads more and more data more and more  
> memory is used - no
> memory is released. As a result, python program  crashes because of  
> little memory available.
> Am I right if I suppose that the memory allocation should not  
> increase during reading data?
> Or am I wrong?
> Thank you again for help

Fix up the things noted first and use Apache's logging facility  
instead of
your own and see if there is a difference.


Graham


More information about the Mod_python mailing list