[Twisted-Python] R: help needed with flow-control for push protocol

Pietro Niccoli piemail at tiscali.it
Thu Nov 5 15:28:22 MST 2009


> Here's some untested code:
> 
>     class SmartEcho(Protocol):
>         implements(IProducer)
> 
>         def pauseProducing(self):
>             self.transport.pauseProducing()
> 
>         def resumeProducing(self):
>             self.transport.resumeProducing()
> 
>         def stopProducing(self):
>             self.transport.stopProducing()
> 
>         def connectionMade(self):
>             self.transport.registerProducer(self, True)
> 
>         def dataReceived(self, bytes):
>             self.transport.write(bytes)
> 
> A careful examination of this code should also reveal the 
> fact that it can be shortened to a more obscure form:
> 
>     class SmartEcho(Protocol):
>         def connectionMade(self):
>             self.transport.registerProducer(self.transport, True)
> 
>         def dataReceived(self, bytes):
>             self.transport.write(bytes)
> 
> Hope this helps,
> Jean-Paul
> 
Thanks a lot. It definitely helped me. I knew how to set up a producer, I
just didn't know that my class was automatically going to be registered as a
consumer for transport. And I didn't know that i could call
transport.registerProducer(self.transport)! Unfortunately twisted is as
powerful as poorly documented sometimes.

Pietro





More information about the Twisted-Python mailing list