Skip to content

Async IO

Ali Ghaffaari edited this page Apr 14, 2020 · 2 revisions

Asynchronous IO has been added in version v1.6.0.

Writing to an asynchronous stream is exactly the same as the synchronous one. However, retrieving protobuf messages from an async stream is a bit different.

High-level method async_parse

It is similar to its sync counterpart parse method, except that it is an async generator. Messages can be retrieved by using async for on the generator:

import stream
import vg_pb2  # or any other compiled protobuf module

async def retrieve():
    # parse from async stream `f`
    async for message in stream.async_parse(f, vg_pb2.Position):
        # work with `message` of type `vg_pb2.Position`
        ...

Low-level method open

This method creates an Stream instance which is an async iterator; i.e. it implements __aiter__, and __anext__. Iterating over Stream async iterator yields messages raw data:

async def retrieve():
    # retrieve message data from async stream `f`
    with open(fileobj=f, mode='rb') as istream:
        async for data in istream:
            message = pb_cls()
            message.ParseFromString(data)
            # work with `message`
            ...

Clone this wiki locally