Search code examples
pythontwistedtwisted.internet

Event when data sent in Twisted Python?


I'm writing a TCP/IP server using the Twisted framework. I am sending a very large stream of data, in small chunks. So I need to know when the data I've sent has made it through Twisted's buffers and into the OS, and the OS is ready for some more data. Is there a way to be notified when this happens?

I am doing this to measure network throughput, so I'm generating an unlimited amount of data.

If I was using normal sockets (not Twisted), the answer would be to use poll() with POLLOUT for that, but I want to do this using Twisted.


Solution

  • Yes.

    self.transport.registerProducer(AProducer(), True), and then

    from zope.interface import implementer
    from twisted.internet.interfaces import IPushProducer
    @implementer(IPushProducer)
    class AProducer:
        def pauseProducing(self):
            "stop producing data; buffers are full"
        def resumeProducing(self):
            "resume producing data; buffers have space"
    

    You can find more in the Twisted documentation for consumers and producers.