Adding print-style function calls, and preproc plugins

Cliff Crawford cjc26 at nospam.cornell.edu
Thu Aug 30 20:33:56 EDT 2001


* Gerson Kurz <gerson.kurz at t-online.de> menulis:
| 
| I came up on the idea during work with my now multithreaded webserver.
| In singlethreaded mode, I redirected sys.stdout to the socket (or
| rather, a cache to be written to that socket) and used print
| statements to nicely generate webpages. Now, in multithreaded mode I
| need a context for the output, so I have to call a function instead;
| which meant that I had to not only replace "print" by "context.write",
| but also enclose the arguments in brackets.

I haven't seen anyone else mention this yet, but if your socket-cache
object is a file-like object (i.e. it implements the methods described
at http://www.python.org/doc/current/lib/bltin-file-objects.html) then
you can do the following (where "obj" is your cache object):

print >>obj, "stuff to send to the socket"
print >>obj, "more stuff"

You can use this form of the print statement to print to any file (or
even to things like StringIO which implement the file object
interface, but aren't real files) without having to reassign sys.stdout.


-- 
Cliff Crawford    ::    http://www.sowrong.org/
"Sheila can keep the balloons away."  -- Thomas



More information about the Python-list mailing list