[Twisted-Python] using defferred?

Mario Ruggier mario at ruggier.org
Wed Jul 9 18:31:56 EDT 2003


I had asked a similar question some time ago--this should surely
be added to the FAQ? (I see it is "mentioned" in the using-twistedweb
howto, but not really explained...)

Here's one way to do it (as I understand it), morphing your
example code to illustrate:

##

from twisted.internet import defer, threads
from twisted.web.server import NOT_DONE_YET

def callbackResult(result, request):
     request.write(result)
     request.finish()

class Simple(resource.Resource):
     isLeaf = True
     def render(self, request):
         d = threads.deferToThread(sendRequest)
         d.addCallback(callbackResult, request)
         return NOT_DONE_YET
...

##

Another way to do it could be to use the woven MVC, in
particular to use the IModel interface. Anyone can provide
a simple example for that?

mario



On mercredi, juil 9, 2003, at 22:17 Europe/Amsterdam, Don Hiatt wrote:

> Howdy,
>
> So I have a simple client that connects to "A" and sends
> a requests and waits for a response. I'm using the clientfactory
> and it works great. Now, I would like to integrate it with
> a webserver, so when the user requests a certain url, I will
> have my client connects to "A", send the request, wait for
> the response, and then render the result in html and pass it
> back to the user. The problem is, how do you do this in a
> event based system? I suspect Defferred might be my friend but
> I'm not sure.
>
> My app looks like
>
> class Simple(resource.Resource):
> 	isLeaf = True
> 	def render(self, request):
> 		sendRequest()
> 		... How do I wait for the client's datarecieved method
> 		.... to be called and then have access to the data??
> 		return result
>
> ....
> __main__
> m = Factory()
> app.connect(h,p,m)
>
> site = server.Site(Simple())
> app.listenTCP(8888, site)
>
>
> Thanks a lot for your time.
>
> Cheers,
>
> don
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python





More information about the Twisted-Python mailing list