[Twisted-Python] echo server using deferToThread

I can't figure out why LineReceived never gets called in the client. Any ideas? --------------------------------------------------------------------- Client Code: from twisted.internet.protocol import ClientFactory from twisted.protocols.basic import LineReceiver from twisted.internet import reactor import sys class EchoClient(LineReceiver): end="GoodBye" def connectionMade(self): self.sendLine("Hello, world!") def lineReceived(self, line): print "receive:", line if line==self.end: self.transport.loseConnection() class EchoClientFactory(ClientFactory): protocol = EchoClient def clientConnectionFailed(self, connector, reason): print 'connection failed:', reason.getErrorMessage() reactor.stop() def clientConnectionLost(self, connector, reason): print 'connection lost:', reason.getErrorMessage() reactor.stop() def main(): factory = EchoClientFactory() reactor.connectTCP('localhost', 8000, factory) reactor.run() if __name__ == '__main__': main() -------------------------------------------------------------------------------- Server Code: from twisted.internet.protocol import Protocol, Factory from twisted.internet import defer, reactor from twisted.python import threadable from twisted.internet import threads threadable.init(1) import time class GetData: def Do(self): time.sleep(3) return 'Done' ### Protocol Implementation # This is just about the simplest possible protocol class Echo(Protocol): def dataReceived(self, data): reactor.callLater(0, self.RunThread, data) def PrintData(self,data): self.transport.write('GoodBye') print "Sent GoodBye" self.transport.loseConnection() # Call reactor.stop() for testing so I don't have to kill -9 the server reactor.stop() def RunThread(self,data): c = GetData() d = threads.deferToThread(c.Do) d.addCallback(self.PrintData) def main(): f = Factory() f.protocol = Echo reactor.listenTCP(8000, f) reactor.run() if __name__ == '__main__': main()

On Sun, Feb 06, 2005 at 07:15:34PM -0800, snacktime wrote:
I can't figure out why LineReceived never gets called in the client. Any ideas? [...]
class EchoClient(LineReceiver):
[...]
You're not writing a whole line, so a line is never received :) Use self.sendLine('GoodBye') instead. -Andrew

On Sun, 6 Feb 2005 20:06:21 -0800, snacktime <snacktime@gmail.com> wrote:
This seems unlikely, since sendLine never blocks. sendLine is nothing more than a call to transport.write() with the appropriate additional bytes tacked on automatically ("\r\n" by default). Echo subclasses Protocol, though. sendLine is a method of LineReceiver. When you call sendLine on your Protocol instance, an AttributeError is raised. The exception is translated into a Failure (so as to be handleable asynchronously). But no handler is ever set up for it, so it goes unreported, unless you get lucky and the Failure gets garbage collected in a timely fashion. So, if you get stuck with a Deferred that appears to just hang forever, make sure you are setting up proper error handling. In some cases, for debugging, this may just mean adding twisted.python.log.err as the errback (`from twisted.python import log; d.addErrback(log.err)'). Jp

On Sun, Feb 06, 2005 at 07:15:34PM -0800, snacktime wrote:
I can't figure out why LineReceived never gets called in the client. Any ideas? [...]
class EchoClient(LineReceiver):
[...]
You're not writing a whole line, so a line is never received :) Use self.sendLine('GoodBye') instead. -Andrew

On Sun, 6 Feb 2005 20:06:21 -0800, snacktime <snacktime@gmail.com> wrote:
This seems unlikely, since sendLine never blocks. sendLine is nothing more than a call to transport.write() with the appropriate additional bytes tacked on automatically ("\r\n" by default). Echo subclasses Protocol, though. sendLine is a method of LineReceiver. When you call sendLine on your Protocol instance, an AttributeError is raised. The exception is translated into a Failure (so as to be handleable asynchronously). But no handler is ever set up for it, so it goes unreported, unless you get lucky and the Failure gets garbage collected in a timely fashion. So, if you get stuck with a Deferred that appears to just hang forever, make sure you are setting up proper error handling. In some cases, for debugging, this may just mean adding twisted.python.log.err as the errback (`from twisted.python import log; d.addErrback(log.err)'). Jp
participants (3)
-
Andrew Bennetts
-
Jp Calderone
-
snacktime