Python 3.7+Async programmingterm
asyncio.BufferedProtocol
protocols and transports
A streaming protocol with a manual buffer: the loop asks for a buffer via get_buffer(), writes data into it and reports the length in buffer_updated(). Added in Python 3.7.
Syntax
class P(asyncio.BufferedProtocol): def get_buffer(self, sizehint): ...; def buffer_updated(self, nbytes): ...
Learners rarely need this layer: streams (asyncio.open_connection() and asyncio.start_server()) cover TCP, and asyncio.create_subprocess_exec() covers processes. BufferedProtocol is an optimisation over Protocol: data lands straight in your bytearray instead of a fresh bytes object per chunk. It only pays off for very large streams; get_buffer() must return a non-empty writable buffer, and you must read exactly nbytes from buffer_updated(), not the whole buffer — the tail holds leftovers from earlier chunks.
Examples
import asyncio
import socket
class Reader(asyncio.BufferedProtocol):
def __init__(self):
self.buf = bytearray(16)
self.chunks = []
def get_buffer(self, sizehint):
return self.buf
def buffer_updated(self, nbytes):
self.chunks.append(bytes(self.buf[:nbytes])) # ровно nbytes, не весь буфер
async def main():
loop = asyncio.get_running_loop()
left, right = socket.socketpair()
transport, proto = await loop.create_connection(Reader, sock=left)
right.sendall(b'hello')
await asyncio.sleep(0.01)
print(proto.chunks) # → [b'hello']
transport.close()
right.close()
asyncio.run(main())
See also
Open in the full glossarydocs.python.orgReport an inaccuracy