<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hi Steve,</div><div class=""><br class=""></div><div class="">It looks like I had marked this message as interesting and warranting a reply, but never got around to it.  I'm sorry it's been quite a while!  I appreciate the amount of research you did here :-).</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Aug 17, 2016, at 3:43 PM, Steve Morin <<a href="mailto:steve.morin@gmail.com" class="">steve.morin@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default"><div class="gmail_default">Twisted Community</div><div class="gmail_default"><br class=""></div><div class="gmail_default"><font face="georgia, serif" class="">Problem: How do you determine the buffer size of a transport, to know how much data is waiting to be transmitted from using transport.write?</font></div><div class="gmail_default"><font face="georgia, serif" class=""><br class=""></font></div><div class="gmail_default"><font face="georgia, serif" class="">Wait! You're going to say: use the Producer Consumer API ( <a href="http://twistedmatrix.com/documents/current/core/howto/producers.html" class="">http://twistedmatrix.com/documents/current/core/howto/producers.html</a> )</font></div></div></div></div></blockquote><div><br class=""></div><div>This is, unfortunately, the only solution :).</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_default"><div class="gmail_default"><font face="georgia, serif" class="">To do what: So that instead of using back pressure I can check the buffer and when it's "too big/full" can decide to do something to the transport I am writing to:</font></div></div></div></div></blockquote><div><br class=""></div>I think when you say "back pressure" you're referring to <i class="">your program</i> exerting back-pressure <i class="">on its peer</i>.  I understand why you don't want to do that.  However, there's another kind of back pressure - <i class="">your peer</i> exerting back pressure <i class="">on your program</i>.</div><div><br class=""></div><div>Commensurately, there are two ways to use back pressure:</div><div><br class=""></div><div><ol class="MailOutline"><li class="">To exert back pressure on your peer, call `self.transport.pauseProducing()`.  Later, when you're ready to receive more data, call `self.transport.resumeProducing()`.  This is what you <i class="">don't</i> want to do.</li><li class="">To detect when back pressure is applied <i class="">from</i> your peer, call `self.transport.registerProducer(self, True)`; then the reactor will call pauseProducing() when its buffer is full and and resumeProducing() when it empties out again.</li></ol><div class=""><br class=""></div><div class="">Your list of things you might want to do here:</div></div><div><br class=""></div><div><blockquote type="cite" class=""><div dir="ltr" class=""><div class="gmail_default"><div class="gmail_default"><span style="font-family: georgia, serif;" class="">- Buffer to disk instead of memory</span></div><div class="gmail_default"><font face="georgia, serif" class="">- Kill the transport</font></div><div class="gmail_default"><font face="georgia, serif" class="">- Decide to skip sending some data</font></div><div class="gmail_default"><font face="georgia, serif" class="">- Send an error or message to the transport I am writing to</font></div><div class="gmail_default"><font face="georgia, serif" class="">- Reduce the resolution, increase the compression (things like video or audio)</font></div></div></div></blockquote><div><br class=""></div></div>is a good one, and all these things can be achieved.  Going through them:<div class=""><br class=""></div><div class=""><ul class="MailOutline"><li class="">If you want to buffer to disk instead of memory, have a method like:<br class=""><br class="">def someDataToSendToPeer(self, someData):<br class="">    if self.isProducing:<br class="">        self.transport.write(someData)<br class="">    else:<br class="">        self.bufferFile.write(someData)<br class=""><br class="">def pauseProducing(self):<br class="">    self.isProducing = False<br class="">    self.bufferFile = open("buffer.file", "wb")<br class=""><br class="">def resumeProducing(self):<br class="">    self.isProducing = True<br class="">    self.startUbufferingFromFile()<br class=""><br class=""></li><li class="">If you want to kill the transport,<br class=""><br class="">def pauseProducing(self):<br class="">    self.transport.abortConnection()<br class=""><br class=""></li><li class="">If you want to reduce video stream quality,<br class=""><br class="">def streamSomeRawVideo(self, someRawVideo):<br class="">    if self.isProducing:<br class="">        self.transport.write(self.videoBuffer.addAndEncodeToBytes(someRawVideo))<br class="">    else:<br class="">        self.videoBuffer.addAndCompressSomeMore(someRawVideo)<br class=""><br class=""></li></ul><div class="">and so on, and so on.</div></div><div class=""><br class=""></div><div class="">Basically, you can treat the buffer as "empty" until pauseProducing() is called.  Once it is, you can treat it as "full".</div><div class=""><br class=""></div><div class="">Hope this was helpful, and still timely enough for you to make some use of it :).</div><div class=""><br class=""></div><div class="">-glyph</div></body></html>