MCPcopy
hub / github.com/msgspec/msgspec / handle_connection

Method handle_connection

examples/asyncio-kv/kv.py:71–90  ·  view source on GitHub ↗

Handle the full lifetime of a single connection

(
        self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
    )

Source from the content-addressed store, hash-verified

69 self.decoder = msgspec.msgpack.Decoder(Request)
70
71 async def handle_connection(
72 self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
73 ):
74 """Handle the full lifetime of a single connection"""
75 print("Connection opened")
76 while True:
77 try:
78 # Receive and decode a request
79 buffer = await prefixed_recv(reader)
80 req = self.decoder.decode(buffer)
81
82 # Process the request
83 resp = await self.handle_request(req)
84
85 # Encode and write the response
86 buffer = self.encoder.encode(resp)
87 await prefixed_send(writer, buffer)
88 except EOFError:
89 print("Connection closed")
90 return
91
92 async def handle_request(self, req: Request) -> Any:
93 """Handle a single request and return the result (if any)"""

Callers

nothing calls this directly

Calls 4

handle_requestMethod · 0.95
prefixed_recvFunction · 0.85
prefixed_sendFunction · 0.85
decodeMethod · 0.80

Tested by

no test coverage detected