[Twisted-web] Using deferreds to render a page

A. Goryunov agoryunov at list.ru
Mon Dec 5 11:16:05 MST 2005


I might be just dumb, but I can't figure out the correct way to do what 
the following passage from Twisted.web's Howto suggests:

"/Request a |Deferred|, return |server.NOT_DONE_YET|, and call 
|request.write("stuff")| and |request.finish()| later, in a callback on 
the |Deferred|/."

How do I get at the request object from a callback function? All the 
documentation on Deferreds that I managed to find uses print's to 
display results, but that doesn't help any, since I intend to present 
output to a remote user rather than myself. For now I've settled with 
using the following method:

class PrintResult(object):

    def __init__(self, request):

        self.request = request

        

    def __call__(self, *args):

        l = args[0]

        if l:

            self.request.write("<html>ID is %s</html>" % str(l[0][0]))

        else:

            self.request.write( "<html>No ID found</html>")

        self.request.finish()

        return server.NOT_DONE_YET


So that the request is stored inside the callback itself, but this 
somehow feels  wrong. Is there some standard way of doing this that I am 
missing?




More information about the Twisted-web mailing list