8000 Set TCP_NODELAY on TCP transports by default · python/asyncio@bea3a42 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit bea3a42

Browse files
committed
Set TCP_NODELAY on TCP transports by default
1 parent 32b8996 commit bea3a42

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

asyncio/selector_events.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ def _test_selector_event(selector, fd, event):
3939
return bool(key.events & event)
4040

4141

42+
if hasattr(socket, 'TCP_NODELAY'):
43+
def _set_nodelay(sock):
44+
if (sock.family in {socket.AF_INET, socket.AF_INET6} and
45+
sock.type == socket.SOCK_STREAM and
46+
sock.proto == socket.IPPROTO_TCP):
47+
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
48+
else:
49+
def _set_nodelay(sock):
50+
pass
51+
52+
4253
class BaseSelectorEventLoop(base_events.BaseEventLoop):
4354
"""Selector event loop.
4455
@@ -640,6 +651,11 @@ def __init__(self, loop, sock, protocol, waiter=None,
640651
self._eof = False
641652
self._paused = False
642653

654+
# Disable the Nagle algorithm -- small writes will be
655+
# sent without waiting for the TCP ACK. This generally
656+
# decreases the latency (in some cases significantly.)
657+
_set_nodelay(self._sock)
658+
643659
self._loop.call_soon(self._protocol.connection_made, self)
644660
# only start reading when connection_made() has been called
645661
self._loop.call_soon(self._loop.add_reader,

0 commit comments

Comments
 (0)
0