Help with Twisted!


If any of you out there have used [[http://twistedmatrix.com/|Twisted]], I need some help! The basic problem is this: I have a server, that needs to talk to another server before it can serve a client request (think of something like a recursive DNS query). Let me throw some pseudo-code to make the problem clear:


def client:
# this will actually be done via a callback, but I simplify for demonstration
print server1.getdata()

def server1:
# server1 has to talk to server2 to get the result
# further, server1 is an XMLRPC server written in twisted
# so the return value of the function is actually returned to the client
# therefore, I _cannot_ use deferreds here because if we jump
# into a callback, we lose context
def getdata():
# the call I make to server2 returns a deferred
# how do I block on it? I tried waitForDeffered, but couldn't get it to work
# examples much appreciated
return server2.callRemote().addCalback(really?)

I don’t know if the code/comments help. Basically my client is talking to an XML-RPC server, which needs to talk to another XML-RPC server to serve the requets. In ”twisted.web.xmlrpc”, calling remote methods via a Proxy object returns a deferred. However, server1 needs to wait for this deferred to fire/finish before it can return to the client. How do I do this? Any ideas?

**Update**: On [[http://www.cs.ucsd.edu/~mvrable/|Mike's]] suggestion, I tried simply returning the Deferred in server1, and guess what, it worked! It seems that when I try to return a Deferred from Server-1, Twisted automatically waits for the Deferred to fire, and returns //its// result instead, which is exactly what I wanted. Ah, the mysteries of asynchronous programming.

Leave a Reply