Skip to content

Commit 13f61bf

Browse files
methanepicnixzvstinner
authored
gh-121313: multiprocessing: simplify by increasing the connection buffer size to 64KiB (GH-123559)
Increases the multiprocessing connection buffer size from 8k to 64k for efficiency, without overallocating. Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 1f4a49e commit 13f61bf

File tree

3 files changed

+5
-17
lines changed

3 files changed

+5
-17
lines changed

Lib/multiprocessing/connection.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import io
1414
import itertools
1515
import os
16-
import stat
1716
import sys
1817
import socket
1918
import struct
@@ -40,7 +39,9 @@
4039
#
4140
#
4241

43-
BUFSIZE = 8192
42+
# 64 KiB is the default PIPE buffer size of most POSIX platforms.
43+
BUFSIZE = 64 * 1024
44+
4445
# A very generous timeout when it comes to local connections...
4546
CONNECTION_TIMEOUT = 20.
4647

@@ -361,11 +362,6 @@ def _get_more_data(self, ov, maxsize):
361362
f.write(ov.getbuffer())
362363
return f
363364

364-
"""
365-
The default size of a pipe on Linux systems is 16 times the base page size:
366-
https://man7.org/linux/man-pages/man7/pipe.7.html
367-
"""
368-
PAGES_PER_PIPE = 16
369365

370366
class Connection(_ConnectionBase):
371367
"""
@@ -378,14 +374,11 @@ def _close(self, _close=_multiprocessing.closesocket):
378374
_close(self._handle)
379375
_write = _multiprocessing.send
380376
_read = _multiprocessing.recv
381-
_default_pipe_size = 0
382377
else:
383378
def _close(self, _close=os.close):
384379
_close(self._handle)
385380
_write = os.write
386381
_read = os.read
387-
_base_page_size = os.sysconf(os.sysconf_names['SC_PAGESIZE'])
388-
_default_pipe_size = _base_page_size * PAGES_PER_PIPE
389382

390383
def _send(self, buf, write=_write):
391384
remaining = len(buf)
@@ -400,13 +393,8 @@ def _recv(self, size, read=_read):
400393
buf = io.BytesIO()
401394
handle = self._handle
402395
remaining = size
403-
is_pipe = False
404-
if size > self._default_pipe_size > 0:
405-
mode = os.fstat(handle).st_mode
406-
is_pipe = stat.S_ISFIFO(mode)
407-
limit = self._default_pipe_size if is_pipe else remaining
408396
while remaining > 0:
409-
to_read = min(limit, remaining)
397+
to_read = min(BUFSIZE, remaining)
410398
chunk = read(handle, to_read)
411399
n = len(chunk)
412400
if n == 0:

Misc/NEWS.d/next/C API/2024-07-03-10-11-53.gh-issue-121313.D7gARW.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Limit the reading size in the :class:`multiprocessing.connection.Connection` class to 64 KiB to prevent memory overallocation and unnecessary memory management system calls.

0 commit comments

Comments
 (0)