[Twisted-Python] help w/simple echo example w/deferred and threads

I am trying to make a very simple echo server use threads to understand how twisted handles blocking code. I think I have the server ok but I am not sure how to have the client grab the data from a thread that finished some time later. If I comment out the sleep call, everything works fine. I looked at deferred but am a little confused. What is the simple code the client missing? Thanks for any help, john Here is the server ############### from twisted.spread import pb from twisted.internet import reactor from twisted.python import threadable threadable.init() import time,random class t: def __init__(self): self.result='' def test(self,st='same'): time.sleep(1) print 'done sleeping' self.result=st+':'+str(time.time()) class Echoer(pb.Root): def __init__(self): self.a=t() def remote_echo(self, st): reactor.callInThread(self.a.test,st) print 'from thread',self.a.result return self.a.result if __name__ == '__main__': reactor.listenTCP(8789, pb.PBServerFactory(Echoer())) reactor.run() #Here is the client from twisted.spread import pb from twisted.internet import reactor, defer from twisted.python import util factory = pb.PBClientFactory() reactor.connectTCP("localhost", 8789, factory) d = factory.getRootObject() d.addCallback(lambda object: object.callRemote("echo", "hello network")) d.addCallback(lambda echo: 'server echoed: '+echo) d.addErrback(lambda reason: 'error: '+str(reason.value)) d.addCallback(util.println) d = factory.getRootObject() d.addCallback(lambda object: object.callRemote("echo", "hello network")) d.addCallback(lambda echo: 'server echoed: '+echo) d.addErrback(lambda reason: 'error: '+str(reason.value)) d.addCallback(util.println) d.addCallback(lambda _: reactor.stop()) -- _______________________________________________ Get your free email from http://www.iname.com
participants (1)
-
john nielsen