[Twisted-Python] echo server using deferToThread
exarkun at divmod.com
Sun Feb 6 23:16:19 EST 2005
On Sun, 6 Feb 2005 20:06:21 -0800, snacktime <snacktime at gmail.com> wrote:
>On Mon, 7 Feb 2005 14:29:33 +1100, Andrew Bennetts
> <andrew-twisted at puzzling.org> wrote:
> > 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):
> > [...]
> > > 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()
> > You're not writing a whole line, so a line is never received :)
> > Use self.sendLine('GoodBye') instead.
> > -Andrew
> replacing self.transport.write with self.sendLine just makes the code
> block when it hits self.sendLine.
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)').
More information about the Twisted-Python