Avoid extraneous copy when `_recv`ing bytes through pipes in multiprocessing "connections"
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Feature or enhancement
Avoid extraneous copy when _recving bytes through pipes in multiprocessing "connections"
Pitch
In the default IPC implementation of the multiprocessing module using pipes, the Connection._recv method is defined as follows:
Seems like we can avoid a copy if we can read directly into a preallocated byte array? This would be beneficial if the bytes we're sending between processes are large. Another benefit here is that the buf.write() call above doesn't release the GIL, so in my workloads I've seen it causing stalls in another thread when trying to read from a multiprocessing.Queue instance.
Sample implementation that avoids the extraneous copy is shown below (bit untested). If it makes sense, I can submit a patch, but probably need some guidance to deal with the Windows case:
import io
def unix_readinto(fd, buf):
# Thanks https://stackoverflow.com/q/13919006/1572989
return io.FileIO(fd, closefd=False).readinto(buf)
class Connection(...):
...
def _recv(self, size, readinto=unix_readinto):
buf = io.BytesIO(initial_bytes=bytes(size))
handle = self._handle
mem = buf.getbuffer()
total = 0
while total != size:
n = readinto(handle, mem[total:])
if n == 0:
if total == 0:
raise EOFError
else:
raise OSError("got end of file during message")
total += n
return buf
Previous discussion
N/A
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với _recv trong Lib/multiprocessing/connection.py, được liên kết trong issue, và so sánh cách việc đọc pipe được xử lý trên Unix và Windows. Xác minh rằng một buffer được cấp phát trước có thể nhận các message lớn mà không cần bản sao bổ sung, đồng thời vẫn giữ nguyên hành vi hiện tại đối với EOF và việc đọc từng phần trên cả hai nền tảng; issue không nêu các test cụ thể.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- distributed-systems, operating-systems
- Loại issue
- Tính năng
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100