[Twisted-Python] Flush socket

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Thu Aug 11 19:43:34 EDT 2011

On 09:54 pm, tobias.oberstein at tavendo.de wrote:
>sorry, my question wasn't clear:
>I do already disable Nagle by setting TCP NoDelay.
>And I do a reactor.select(0), which sometimes breaks I guess
>because of reactor reentry not expected, and it will break
>when the reactor is not select() based I guess.
>So the question is: is there an alternative to reactor.select(0)
>after a transport.write() to make the reactor call write() on
>the underlying socket for all stuff buffered within Twisted?

Sure.  Implement your own IFileDescriptor.  When you add the descriptor 
to the reactor using IReactorFDSet.addWriter, it will call back onto 
your object's doWrite method to tell it there is space in the write 
buffer.  Then you can write exactly as many bytes as you want, as well 
as implementing other policies - for example you can remove the 
descriptor from the reactor's write set using 
IReactorFDSet.removeWriter, thus delaying future writes until it is re- 

This will always be a somewhat unreliable way to test a remote process's 
handling of packetization, since there are still two TCP/IP stacks which 
can mess around with the data in a variety of ways, but it's as good as 
you can do if you want to use normal sockets for this testing.

A more reliable way might be to synthesize the IP datagrams yourself, 
and inject them into the recipient's TCP/IP stack.  Or skip the TCP/IP 
stack and inject them into the recipient process directly, by replacing 
the BSD socket APIs with an alternate implementation you control 
(perhaps using an LD_PRELOAD hook, for example).

Twisted doesn't offer much in the way of assistance for those latter 
approaches, though.

>On 11.08.11 23:47, "Itamar Turner-Trauring" <itamar at itamarst.org> 
>>so there is no Twisted sanctioned (reactor independent and reentry 
>>alternative to disable nagle (tcp nodelay) and doing a select()?
>You can disable nagle in Twisted, if that's the question
>(transport.setTcpNoDelay(True)). But you can't determine TCP packet 
>that's up to the operating system.
>Twisted-Python mailing list
>Twisted-Python at twistedmatrix.com

More information about the Twisted-Python mailing list