summaryrefslogtreecommitdiffstats
path: root/Lib/multiprocessing
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2014-02-08 22:05:52 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2014-02-08 22:05:52 (GMT)
commit0c1e31ab1ecc6a5b6662fad62e0ede1019741f6a (patch)
treec95f28f0208c37124f27435ae787850aeb89298f /Lib/multiprocessing
parent2c66918462dc59c964c9a0fcfec34711711c5fc4 (diff)
parentb7d6d2ac6ea4361fd72314d466029bd119ad3fea (diff)
downloadcpython-0c1e31ab1ecc6a5b6662fad62e0ede1019741f6a.zip
cpython-0c1e31ab1ecc6a5b6662fad62e0ede1019741f6a.tar.gz
cpython-0c1e31ab1ecc6a5b6662fad62e0ede1019741f6a.tar.bz2
Issue #20540: Fix a performance regression (vs. Python 3.2) when layering a multiprocessing Connection over a TCP socket.
For small payloads, Nagle's algorithm would introduce idle delays before the entire transmission of a message.
Diffstat (limited to 'Lib/multiprocessing')
-rw-r--r--Lib/multiprocessing/connection.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py
index 8ad6fd5..09ec4fc 100644
--- a/Lib/multiprocessing/connection.py
+++ b/Lib/multiprocessing/connection.py
@@ -395,13 +395,23 @@ class Connection(_ConnectionBase):
return buf
def _send_bytes(self, buf):
- # For wire compatibility with 3.2 and lower
n = len(buf)
- self._send(struct.pack("!i", n))
- # The condition is necessary to avoid "broken pipe" errors
- # when sending a 0-length buffer if the other end closed the pipe.
- if n > 0:
- self._send(buf)
+ # For wire compatibility with 3.2 and lower
+ header = struct.pack("!i", n)
+ if n > 16384:
+ # The payload is large so Nagle's algorithm won't be triggered
+ # and we'd better avoid the cost of concatenation.
+ chunks = [header, buf]
+ elif n > 0:
+ # Issue # 20540: concatenate before sending, to avoid delays due
+ # to Nagle's algorithm on a TCP socket.
+ chunks = [header + buf]
+ else:
+ # This code path is necessary to avoid "broken pipe" errors
+ # when sending a 0-length buffer if the other end closed the pipe.
+ chunks = [header]
+ for chunk in chunks:
+ self._send(chunk)
def _recv_bytes(self, maxsize=None):
buf = self._recv(4)