Module

_sendfile

Zero-copy sendfile support for static file serving.

Provides an async callable that usesloop.sendfile()to transfer file data directly from the filesystem to a socket, bypassing Python memory.

Constraints:

  • Only works on non-TLS connections (SSL wraps the socket, preventing sendfile)
  • Unix-like systems only (Linux, macOS, FreeBSD)
  • Falls back gracefully when unavailable

Back-pressure: asyncio sets transport sockets non-blocking, so a raw os.sendfile loop raises BlockingIOError(EAGAIN) the moment the kernel send buffer fills — a normal flow-control signal that crashes a hand-rolled loop.loop.sendfile(transport, ...)is the transport-aware primitive: it detaches the live transport from the selector, runs native sendfile with proper EAGAIN /add_writerretry handling, then restores the transport. See https://github.com/lbliii/pounce/issues/72.

Classes

SendfileRegion 4
Protocol-owned file body marker. h11's passthrough send path only needs ``len(data)`` for body acc…

Protocol-owned file body marker.

h11's passthrough send path only needslen(data)for body accounting. The ASGI bridge recognizes this marker and transfers the referenced file range withos.sendfileafter writing h11's surrounding framing bytes.

Attributes

Name Type Description
path Path
offset int
count int

Methods

Internal Methods 1
__len__ 0 int
def __len__(self) -> int
Returns
int

Functions

can_use_sendfile 1 bool
Check if sendfile can be used on this connection. Returns False for TLS connec…
def can_use_sendfile(writer: asyncio.StreamWriter) -> bool

Check if sendfile can be used on this connection.

Returns False for TLS connections and when the raw socket is unavailable.

Parameters
Name Type Description
writer asyncio.StreamWriter
Returns
bool
create_sendfile_callable 1 SendfileCallable
Create an async sendfile callable bound to this writer's transport. The return…
def create_sendfile_callable(writer: asyncio.StreamWriter) -> SendfileCallable

Create an async sendfile callable bound to this writer's transport.

The returned callable transfers file data to the socket using loop.sendfile(), which handles non-blocking-socket back-pressure (EAGAIN) via the selector instead of crashing in an executor thread.

Parameters
Name Type Description
writer asyncio.StreamWriter

The asyncio StreamWriter for the connection.

Returns
SendfileCallable