[mod_python] better understanding of the req object

Mike Looijmans nlv11281 at natlab.research.philips.com
Tue Jun 5 03:02:00 EDT 2007


Tip: Output the "heading" of your page first and "flush" it to the user as soon as possible. Then 
run the queries and feed him the results.

Sending the header quickly prevents the user accidentally submitting a web form twice (he'll get 
impatient) and provides fast feedback that we're processing his request. Gives the website a fast 
appearance too.

If the query results tend to come in small chunks (e.g. sequential scan with few matches), i usually 
output the data one row at a time (one req.write() for each <tr>...</tr> part). When outputting 
large tables, i usually send out tens of rows at a time.

Buffering parts of the page in strings or list is in general a good idea. Buffering the whole page 
is, in my opinion, bad practice. The user presses a button, then nothing seems to happen for a 
while, and then suddenly the new page appears. I prefer to respond to the button press as quickly as 
possible, even though the new page will just be a title for the next two seconds.


Mike Looijmans
Philips Natlab / Topic Automation


Graham Dumpleton wrote:
> The only problem with using the 'print' method as shown is that there
> is no way to control buffering. As a result, every one of those little
> strings gets written out by Apache separately which can slow down
> throughput.
> 
> It is actually better to use the following if outputing lots of little
> strings using 'print'.
> 
>  import StringIO
> 
>  data = StringIO.StringIO()
> 
>  print >> data, "the uri is", req.uri
> 
>  req.write(data.getvalue())
> 
> Ie., perform only one call to write() with data accumulated using
> StringIO (or cStringIO).
> 
> Graham
> 
> On 05/06/07, David Bear <David.Bear at asu.edu> wrote:
> 
>> Thanks.
>>
>> I was thinking in terms of using ',' string catenation like
>> print 'this ', 'is something'.. while using req.write method.
>>
>> I hate having to keep learning the same thing over and over...
>>
>> On Mon, Jun 04, 2007 at 04:47:48PM -0700, David Schachter wrote:
>> > David--
>> >
>> > Try one of these:
>> >
>> > req.write("the uri is %s" % req.uri)
>> > print >> req, "the uri is %s" % req.uri
>> > print >> req, "the uri is", req.uri
>> >
>> > The latter two forms use the fact that the req object has a write()
>> > method compatible with what the print keyword expects. It's a bit nicer
>> > than the first form, imho.
>> >
>> > You tried to pass two strings to a function that takes only one string
>> > and an optional boolean flag (flush=0). You are lucky the universe did
>> > not invert into a black hole. Or perhaps unlucky; it depends on your
>> > point of view, I suppose.
>> >
>> >                      -- David Schachter
>> >
>> > David Bear wrote:
>> > >I'm trying to understand all the members/methods of the req object but
>> > >anytime I try something like
>> > >
>> > >req.write('the uri is ', req.uri)
>> > >
>> > >or req.write('the hostname is ', req.hostname)
>> > >
>> > >I get an TypeError.
>> > >
>> > >I'm completely not understanding how to work with the req object.
>> > >
>> > >I assumed members would be 'callable' and return their value.
>> > >
>> > >
>>
>> -- 
>> David Bear
>> phone:  602-496-0424
>> fax:    602-496-0955
>> College of Public Programs/ASU
>> University Center Rm 622
>> 411 N Central
>> Phoenix, AZ 85007-0685
>>  "Beware the IP portfolio, everyone will be suspect of trespassing"
>> _______________________________________________
>> Mod_python mailing list
>> Mod_python at modpython.org
>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
> 
> 




More information about the Mod_python mailing list