[mod_python] Multiple returns to client

Martijn Moeling martijn at xs4us.nu
Wed Sep 6 11:16:03 EDT 2006


I had similair behaviour when I started working with mod_python,
After some digging in the mod_python docs I found that it had to do with
global vars in the python module where the handler is defined.

Something like:

Host = ""
Def handler(req)
	Global host
	Host=req.host.split(".")[0]

I solved this by creating a class which is created and destroyed in the
handler, within the init of the class I set the database connection and
I can use global vars


Class visitor:
	Def __init__(self):
		Self.Db.init
		Self.var=""
	
	Def myhandler(Self,req):
		#Do all the stuff you want
		Return apache.OK
8<-------------------------------

Import visitors

Def handler(req):
    Vis_con=visitor()
    Result = Vis_con.myhandler(req)
    Return result

(this is rough unchecked code but just explains my point)

-----Original Message-----
From: mod_python-bounces at modpython.org
[mailto:mod_python-bounces at modpython.org] On Behalf Of Jim Gallacher
Sent: Wednesday, September 06, 2006 1:45 PM
To: Ethan Toan Ton
Cc: mod_python at modpython.org
Subject: Re: [mod_python] Multiple returns to client

Ethan Toan Ton wrote:
> Mike,
> 
> Thanks for the help.  req.flush() works for sending out the data. 
> Unfortunately, at the same time, my python program is making requests
and
> receiving data from other http sources at the same time.  When I run
the
> req.flush(), it outputs some of the incoming data as well.  Not sure
why
> that is happening.

That doesn't make any sense at all. Perhaps you can give a concrete 
example? Method req.flush() is a simple wrapper around the Apache 
ap_rflush() call.

Note that you can also use req.write(some_string, flush=1) to 
immediately flush the output. For flush=1 causes ap_rflush() to be 
called as well.

Jim

> Ethan
> 
> 
>> Your output is buffered in multiple stages. In most cases, you can
call
>> "req.flush()" to send data to the client. This will usually work,
unless
>> you have a module or a filter installed that collects output until
the
>> final stage.
>>
>> For example, this pattern shows how to send output from a slow query
>> back to the user, so that he can see the results as they come back.
By
>> calling req.flush() at smart points, we make sure there's some
feedback
>> quite efficiently.
>>
>> ...
>>
req.write("<P>Results:</P><table><tr><th>Name</th><th>Date</th></tr>")
>> req.flush()
>> c=database.cursor()
>> c.execute("SELECT name,date FROM persons")
>> while 1:
>>     data = c.fetchmany()
>>     if not data:
>>         break
>>     for row in data:
>>         req.write("<tr><td>%s</td><td>%s</td></tr>" % row)
>>     req.flush()
>> req.write("</table>")
>> ...
>>
>>
>> Mike Looijmans
>> Philips Natlab / Topic Automation
>>
>>
>> Ethan Toan Ton wrote:
>>> I was wondering if anyone knew of a way to return multiple times to
a
>>> client using a Mod-Python event handler.  Using the req.write()
method
>>> does nothing until the actual return statement sends all the data in
one
>>> message back to the client.  So before you get to the actual return
>>> statement, can you output back to them more than once?
>> _______________________________________________
>> 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
> 

_______________________________________________
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