Module

async_pool

AsyncPool — dedicated event loop for streaming and WebSocket connections.

Receives handoffs from SyncWorkers when the ASGI app returns a streaming response (more_body=True) or WebSocket upgrade. Wraps the socket in asyncio streams and continues the ASGI lifecycle.

Classes

StreamingHandoff 4
Handoff for HTTP streaming response (more_body=True).

Handoff for HTTP streaming response (more_body=True).

Attributes

Name Type Description
conn socket.socket
scope dict[str, Any]
body bytes
request_id str | None
WebSocketHandoff 5
Handoff for WebSocket upgrade.

Handoff for WebSocket upgrade.

Attributes

Name Type Description
conn socket.socket
request RequestReceived
client tuple[str, int]
server tuple[str, int]
scope dict[str, Any]
AsyncPool 8
Dedicated event loop for streaming/SSE/WebSocket connections. Accepts handoffs from SyncWorker thr…

Dedicated event loop for streaming/SSE/WebSocket connections.

Accepts handoffs from SyncWorker threads via a thread-safe queue. Wraps sockets in asyncio streams and runs the ASGI app.

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
accept_handoff 1
Accept a handoff from a SyncWorker (thread-safe).
def accept_handoff(self, handoff: HandoffRequest) -> None
Parameters
Name Type Description
handoff
run 0
Run the event loop until shutdown (blocking).
def run(self) -> None
Internal Methods 5
__init__ 5
def __init__(self, config: ServerConfig, app: ASGIApp, *, shutdown_event: threading.Event | None = None, ssl_context: ssl.SSLContext | None = None, lifecycle_collector: LifecycleCollector | None = None) -> None
Parameters
Name Type Description
config
app
shutdown_event Default:None
ssl_context Default:None
lifecycle_collector Default:None
_serve 0
Event loop: process handoffs until shutdown.
async
async def _serve(self) -> None
_handle_handoff_async 1
Handle a handoff (async task).
async
async def _handle_handoff_async(self, handoff: HandoffRequest) -> None
Parameters
Name Type Description
handoff
_handle_streaming_handoff 1
Handle HTTP streaming handoff: wrap socket, run app from scratch.
async
async def _handle_streaming_handoff(self, handoff: StreamingHandoff) -> None
Parameters
Name Type Description
handoff
_handle_websocket_handoff 1
Handle WebSocket handoff: wrap socket, run handle_websocket.
async
async def _handle_websocket_handoff(self, handoff: WebSocketHandoff) -> None
Parameters
Name Type Description
handoff

Functions

_create_h1_protocol 1 H1Protocol
Create an HTTP/1.1 protocol handler.
def _create_h1_protocol(*, max_incomplete_event_size: int | None = None) -> H1Protocol
Parameters
Name Type Description
max_incomplete_event_size int | None Default:None
Returns
H1Protocol