Zero-Copy Transfers with os.sendfile
Standard file streaming copies data between disk cache, user-space Python memory, and kernel socket buffers. `os.sendfile()` moves bytes directly between file descriptors inside the OS kernel without user-space round trips 🏎️.
import os
import socket
import tempfile
with tempfile.NamedTemporaryFile() as tmp:
tmp.write(b"STREAM_DIRECT_DATA")
tmp.flush()
s1, s2 = socket.socketpair()
# Stream 18 bytes straight through kernel page cache to socket:
bytes_sent = os.sendfile(s1.fileno(), tmp.fileno(), 0, 18)
received = s2.recv(18)
print(f"Sent: {bytes_sent} bytes -> {received.decode()} 🏎️")
s1.close()
s2.close()
# 82. Zero-Copy Transfers with os.sendfile Standard file streaming copies data between disk cache, user-space Python memory, and kernel socket buffers. `os.sendfile()` moves bytes directly between file descriptors inside the OS kernel without user-space round trips 🏎️. ```python import os import socket import tempfile with tempfile.NamedTemporaryFile() as tmp: tmp.write(b"STREAM_DIRECT_DATA") tmp.flush() s1, s2 = socket.socketpair() # Stream 18 bytes straight through kernel page cache to socket: bytes_sent = os.sendfile(s1.fileno(), tmp.fileno(), 0, 18) received = s2.recv(18) print(f"Sent: {bytes_sent} bytes -> {received.decode()} 🏎️") s1.close() s2.close() ``` High-throughput static file servers like Nginx and Caddy rely heavily on `sendfile()` to saturate multi-gigabit network cards with near-zero CPU utilization ⚡! --- ### Question Why is `os.sendfile()` termed 'zero-copy' compared to traditional `f.read()` and `sock.send()` loops? 🧠 - [ ] a. Data is copied directly between OS page cache and socket buffers inside kernel space, bypassing user-space RAM 🏎️ - [ ] b. The data file is mathematically reduced to zero bytes prior to network transmission - [ ] c. The file descriptor table is cloned into the hardware network card directly - [ ] d. It bypasses TCP handshakes and sends unverified UDP datagrams --- ### Challenge Write `b'ZERO_COPY_PAYLOAD'` to a `tempfile.NamedTemporaryFile`. Use `socket.socketpair()` and `os.sendfile` to stream the 17 bytes from `tmp` to `s1`. Recv from `s2` and print the decoded string. 🎯 <details> <summary>Solution</summary> ```python import os import socket import tempfile with tempfile.NamedTemporaryFile() as tmp: tmp.write(b'ZERO_COPY_PAYLOAD') tmp.flush() s1, s2 = socket.socketpair() os.sendfile(s1.fileno(), tmp.fileno(), 0, 17) msg = s2.recv(17).decode() print(msg) s1.close() s2.close() ``` </details>