Module

sync_worker

SyncWorker — blocking I/O worker for request-response workloads.

One request at a time per thread, no asyncio. On 3.14t, runs in a thread with true parallelism. Handles HTTP/1.1 keep-alive in a tight recv/send loop.

When the ASGI app returns a streaming response (more_body=True) or WebSocket upgrade, raises NeedsAsyncError — the supervisor hands off to the async pool (Phase 2). For Phase 1, streaming requests receive 501 Not Implemented.

Classes

_RequestMeta 1
Pre-extracted header values from a single pass over request headers. All fields populated by ``_cl…

Pre-extracted header values from a single pass over request headers.

All fields populated by_classify_request()— avoids redundant linear scans per request. Header names from_fast_h1are already lowered, so no.lower()is needed on names.

Methods

Internal Methods 1
__init__ 0
def __init__(self) -> None
SyncWorker 12
Blocking I/O worker — one request at a time, no event loop. On 3.14t, runs in a thread with true p…

Blocking I/O worker — one request at a time, no event loop.

On 3.14t, runs in a thread with true parallelism. Handles HTTP/1.1 keep-alive in a tight recv/send loop. Streaming and WebSocket require handoff to the async pool (Phase 2).

Methods

set_lifespan_state 1
Set the lifespan state dict shared with all requests.
def set_lifespan_state(self, state: dict[str, Any]) -> None
Parameters
Name Type Description
state
start_draining 0
Mark this worker as draining (stop accepting new connections).
def start_draining(self) -> None
is_idle 0 bool
True if no connection is currently being handled.
def is_idle(self) -> bool
Returns
bool
run 0
Accept connections until shutdown (blocking).
def run(self) -> None
Internal Methods 8
__init__ 10
def __init__(self, config: ServerConfig, app: ASGIApp, sock: socket.socket | None, *, worker_id: int = 0, shutdown_event: threading.Event | None = None, ssl_context: ssl.SSLContext | None = None, lifecycle_collector: LifecycleCollector | None = None, async_pool: AsyncPool | None = None, conn_queue: queue.Queue[tuple[socket.socket, object]] | None = None, sync_app: SyncApp | None = None) -> None
Parameters
Name Type Description
config
app
sock
worker_id Default:0
shutdown_event Default:None
ssl_context Default:None
lifecycle_collector Default:None
async_pool Default:None
conn_queue Default:None
sync_app Default:None
_run_from_queue 2
Get connections from distributor queue (no thundering herd).
def _run_from_queue(self, poll_interval: float, runner: asyncio.Runner) -> None
Parameters
Name Type Description
poll_interval
runner
_run_accept_loop 2
Accept directly from socket (SO_REUSEPORT or single worker).
def _run_accept_loop(self, poll_interval: float, runner: asyncio.Runner) -> None
Parameters
Name Type Description
poll_interval
runner
_handle_connection 3
Handle a single TCP connection through request-response cycles.
def _handle_connection(self, conn: socket.socket, addr: tuple[str, int], runner: asyncio.Runner) -> None
Parameters
Name Type Description
conn
addr
runner
_handle_connection_impl 3 bool
Inner connection handling. Returns True if handed off to async pool.
def _handle_connection_impl(self, conn: socket.socket, addr: tuple[str, int], runner: asyncio.Runner) -> bool
Parameters
Name Type Description
conn
addr
runner
Returns
bool
_recv_request_fast 1 tuple[RequestReceived | …
Read a complete HTTP request using the fast parser. Accumulates data in the re…
def _recv_request_fast(self, conn: socket.socket) -> tuple[RequestReceived | None, bytes]

Read a complete HTTP request using the fast parser.

Accumulates data in the recv buffer until a complete request (headers + body based on Content-Length) is available. Returns (request, body) or (None, b"") on connection close/error.

Parameters
Name Type Description
conn
Returns
tuple[RequestReceived | None, bytes]
_cached_date_header 0 bytes | None
Return cached Date header bytes, refreshing at most once per second.
def _cached_date_header(self) -> bytes | None
Returns
bytes | None
_send_error 3
Send a plain-text error response (raw bytes, no h11).
def _send_error(self, conn: socket.socket, status: int, message: str) -> None
Parameters
Name Type Description
conn
status
message

Functions

_classify_request 1 _RequestMeta
Single-pass header extraction for the sync-worker hot path. Replaces separate …
def _classify_request(request: RequestReceived) -> _RequestMeta

Single-pass header extraction for the sync-worker hot path.

Replaces separate calls to_wants_close, _is_websocket_upgrade, andget_header(accept-encoding). Header names are already lowered by_fast_h1.parse_request.

Parameters
Name Type Description
request RequestReceived
Returns
_RequestMeta