I'm looking for some 'beyond basic' guidance on usage patterns for the StreamReader and StreamWriter classes in the Python asyncio package.
I am attempting to build a stateful server with a custom protocol using protobuf. Should I be sub-classing the StreamReader and StreamWriter to manage the serialization from protobuf bytes? I could then provide a read_message function on the reader. I know I can copy the code from streams.start_server providing my own StreamReader, but how do I set my StreamWriter?
Any pointers or examples gratefully received.
I found it relatively straightforward to subclass the asyncio.streams library classes.
The start_server function is lifted from the tcp server example:
It was necessary to subclass StreamReaderProtocol in order to construct my own StreamWriter. Other than that this is the same as the library function.
For outgoing messages:
And for incoming messages:
Hope it helps someone