[mod_python] better understanding of the req object

Graham Dumpleton graham.dumpleton at gmail.com
Mon Jun 4 20:15:02 EDT 2007


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
>


More information about the Mod_python mailing list