(original) (raw)
changeset: 103674:3f7e4ae9eba3 branch: 3.5 parent: 103671:f12a59311885 user: Yury Selivanov yury@magic.io date: Sun Sep 11 21:39:31 2016 -0400 files: Lib/asyncio/selector_events.py Misc/NEWS description: Issue #27456: asyncio: Set TCP_NODELAY by default. diff -r f12a59311885 -r 3f7e4ae9eba3 Lib/asyncio/selector_events.py --- a/Lib/asyncio/selector_events.py Sun Sep 11 21:11:02 2016 -0400 +++ b/Lib/asyncio/selector_events.py Sun Sep 11 21:39:31 2016 -0400 @@ -39,6 +39,17 @@ return bool(key.events & event) +if hasattr(socket, 'TCP_NODELAY'): + def _set_nodelay(sock): + if (sock.family in {socket.AF_INET, socket.AF_INET6} and + sock.type == socket.SOCK_STREAM and + sock.proto == socket.IPPROTO_TCP): + sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) +else: + def _set_nodelay(sock): + pass + + class BaseSelectorEventLoop(base_events.BaseEventLoop): """Selector event loop. @@ -640,6 +651,11 @@ self._eof = False self._paused = False + # Disable the Nagle algorithm -- small writes will be + # sent without waiting for the TCP ACK. This generally + # decreases the latency (in some cases significantly.) + _set_nodelay(self._sock) + self._loop.call_soon(self._protocol.connection_made, self) # only start reading when connection_made() has been called self._loop.call_soon(self._loop.add_reader, diff -r f12a59311885 -r 3f7e4ae9eba3 Misc/NEWS --- a/Misc/NEWS Sun Sep 11 21:11:02 2016 -0400 +++ b/Misc/NEWS Sun Sep 11 21:39:31 2016 -0400 @@ -254,6 +254,8 @@ - Issue #21201: Improves readability of multiprocessing error message. Thanks to Wojciech Walczak for patch. +- Issue #27456: asyncio: Set TCP_NODELAY by default. + IDLE ---- /yury@magic.io