[Twisted-Python] A Twisted Web Question....

JD lists at webcrunchers.com
Sun Dec 28 19:57:20 EST 2003

in my resource's "render" method,  I'm doing some things that require a 
fairly long time to
accomplish,  so each time through a loop,   I want to render some 
intermediate results
to the brower.

Kinda like this:

class myResource(resource.Resource):
	isLeaf = True
	def	render(self, request):
		# Now I build up a string to render to the browser
		html = make_body()

		# And now I want to go into a loop
		for item in items:
			result = compute_some_item(item)   # I just do something that 
returns some result
			request.write(result)				# then send it to the browser
			return(server.NOT_DONE_YET)	# Do I return now?    If so,   will I 
re-enter my "render" method again

		# Now,  do I just return the html I generated earlier

what happens when I return with server.NOT_DONE_YET?   will I get 
called again so I can do the

What is the sequence?

According to the documentation,  I quote:  (from render.py)

     def render(self, request):
         """Render a given resource.

         This must be implemented in all subclasses of Resource.

         The return value of this method will be the rendered page, 
unless the
         return value is twisted.web.server.NOT_DONE_YET, in which case 
it is
         this class's responsibility to write the results to
         request.write(data), then call request.finish().

According to this,   I return server.NOT_DONE_YET...    but then it 
goes on to say
it's my classes responsibility to write the results to 
request.write(data),   to write
the results,   but how do i do that,   because if i return 
then I've exited,  and have no opportunity to call 

So when do I call it?    Do I call it BEFORE I return?   If so,  then 
why doesn't
the bloody document say that?

And,   in the using Web document,  it says....

A Resource object may do various things to produce output which will be 
sent back to the browser:

*	Return a string
*	Call request.write("stuff") as many times as desired, then call 
request.finish() and return server.NOT_DONE_YET (This is deceptive, 
since you are in fact done with the request, but is the correct way to 
do this)
*	Request a Deferred, return server.NOT_DONE_YET, and call 
request.write("stuff") and request.finish() later, in a callback on the 

This seems a little more reasonable,  and my question,  according to 
this is....   do I do it in this order?

1) call request.write(html)    # from above

Then for each interation of some loop:
1) call request.write(stuff) a bunch of times,  which for each 
iteration of a loop,  may take up to a minute.
2) call request.finish()
3) return server.NOT_DONE_YET

Is this the right order for doing this?

Should I try to use a Deferred?    I have no clue how to do that,   
there appears to be a severe lack of
examples similar to what I want to do.

Lets say I want to defer ONE interation of a loop...    Lets call this 

def   do_iteration(item):
	# do the interation

So,   would I construct my loop like this:

# In the render method.
for item in items:
	# process some item

	# Then do something to add this deferred,   which I still don't know 
how to do.
          # something like this?

          reactor.callLater(something, do_iteration)

         # But since I'm in the "render" method,  how do I get a 
reference to "reactor"?
         # And what it the meaning of "something" in the first argument 
of "callLater"?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 5124 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20031228/7e156c8e/attachment.bin 

More information about the Twisted-Python mailing list