Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 44c19ec

Browse files
committed
Issue #27456: asyncio: Set TCP_NODELAY by default.
1 parent a05a6ef commit 44c19ec

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

Lib/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,

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ Library
254254
- Issue #21201: Improves readability of multiprocessing error message. Thanks
255255
to Wojciech Walczak for patch.
256256

257+
- Issue #27456: asyncio: Set TCP_NODELAY by default.
258+
257259
IDLE
258260
----
259261

0 commit comments

Comments
 (0)