Low-Level Transports & Protocols in asyncio
While `StreamReader` and `StreamWriter` wrap coroutines conveniently, high-throughput engines use `asyncio.Protocol` and `Transport`. The loop invokes pure synchronous callbacks (`data_received`, `connection_made`), eliminating coroutine allocation overhead per network packet 🏎️⚡.
import asyncio
class FastEchoProtocol(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
def data_received(self, data):
# Zero coroutine allocations: synchronous callback
self.transport.write(b"ACK:" + data)
self.transport.close()
async def main():
loop = asyncio.get_running_loop()
server = await loop.create_server(FastEchoProtocol, "127.0.0.1", 0)
port = server.sockets[0].getsockname()[1]
r, w = await asyncio.open_connection("127.0.0.1", port)
w.write(b"PING")
await w.drain()
print(f"Reply: {(await r.read(100)).decode()} 🚀")
w.close()
await w.wait_closed()
server.close()
await server.wait_closed()
asyncio.run(main())
# 28. Low-Level Transports & Protocols in asyncio When building high-concurrency proxies or custom binary network engines, high-level `asyncio.StreamReader` adds coroutine wrapping overhead for every incoming packet 🛑. ```python import asyncio class MicroProtocol(asyncio.Protocol): def connection_made(self, transport): self.transport = transport def data_received(self, data): self.transport.write(b"PONG") ``` ### The Two-Layer Model 1. **Transport Layer**: Abstracts the physical network socket, handling byte transmission, backpressure draining, and OS socket lifecycle 🌐. 2. **Protocol Layer**: Implements the application state machine through event callbacks (`connection_made`, `data_received`, `connection_lost`). 3. **Zero Coroutine Overhead**: Callbacks run directly in the event loop without the overhead of instantiating `async def` frame objects for every single packet arrival 🏎️. --- ### Interactive Pop-Quiz 🧠 #### What is the primary architectural difference between asyncio Streams and the Protocol/Transport model? - [ ] Protocols use callback-driven hooks without creating coroutine suspension frames on every read/write 🏎️ - [ ] Transports run exclusively inside Linux kernel ring 0 - [ ] Protocols support only UDP datagrams while Streams support only HTTP/3 - [ ] Transports require multithreaded hardware locks for every packet --- ### Practical Challenge 🎯 Subclass `asyncio.Protocol` with a protocol `UpperProtocol` that on `data_received(data)` writes back `data.upper()` and closes its transport. Start the server on port 0, connect with `open_connection`, send `b'hello world'`, read the uppercase response, close client and server, and print the decoded response. <details> <summary>Solution</summary> ```python import asyncio class UpperProtocol(asyncio.Protocol): def connection_made(self, transport): self.transport = transport def data_received(self, data): self.transport.write(data.upper()) self.transport.close() async def main(): loop = asyncio.get_running_loop() server = await loop.create_server(UpperProtocol, "127.0.0.1", 0) port = server.sockets[0].getsockname()[1] r, w = await asyncio.open_connection("127.0.0.1", port) w.write(b"hello world") await w.drain() res = await r.read(100) w.close() await w.wait_closed() server.close() await server.wait_closed() print(res.decode()) asyncio.run(main()) ``` </details>