Yes,
the problem is not whatever class or method you use to send the output
to the client, but the problem is the use of the global sys.stdout
variable. The is only one such object in your system. If there are two
threads each handling a request, there is no telling to which one the
sys.stdout.write or print statement will send its output. To prevent
this, you'd have to mutex access to it, and leads to being able to
handle only one request at a time.
On many unix flavors, apache defaults to 'forking', which runs each
request in a separate process. In these circumstances, you will not
detect this problem. On other platforms, or if you modify the httpd.conf
file, apache uses threading or even a mix of threading and forking.
Important note: Since print sends to sys.stdout, using print is just as
bad a method to send output to the client as sys.stdout.write.
The only solution is to use the context in your handler, i.e.
WRITE("hello world\n")
or the more obvious (like in publisher and PSP):
req.write("Hello world\n")
Anything that makes use of globals to send back data to the client will
not work, so "wepi.write" is also out of the question (if wepi is a module).
--
Mike Looijmans
Philips Natlab / Topic Automation
Fırat KÜÇÜK wrote:
import sys
sys.stdout.write(open('penguin.png', 'rb').read())
NO. NO. NO.
sys.stdout is a global variable. If you want sys.stdout.write() to end
up in the user's terminal, your web server will be able to either
serve only one request at a time, or must be forced into running
separate processes for each concurrent request (which will not happen
on windows and many other supported platforms).
We made that mistake with cgipython, and we won't make it again. Learn
from the mistakes of others, as you do not have the time to make them
all yourself.
Mike
Hi,
but i replaced sys.stdout with a StdOut class. is it problem too?
Fırat KÜÇÜK