-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Async support for the splitstream Python library would be much appreciated.
Python I/O applications are increasingly moving to rely on the async/await semantics and so far there is no way of using splitstream for reading JSON objects in these cases.
Specifically, async support for me means a new variant of splitfile, which:
- accepts
filelike.read(n)returning an awaitable object. This would match both theasyncio.StreamReaderinterface and theaiofiles.(File)interface, covering most async use cases for splitstream. - either:
- exposes an async iterator instead of a normal iterator.
callback()may return an awaitable object which will be awaited before continuing to read.
There are two obvious ways of providing async support:
- Expose the basic C interface of splistream to Python. Via
SplitstreamGetNextDocument, callers can pass in their own data. Async variants of splistream can then be implemented in Python on top of this interface. An asyncsplitfile()should probably be implemented in this project, so that there is a reliable reference implementation for the most common case. - Implement an
async_splitfile()function (name to be decided) directly in C by implementing the async iterator protocol.
Possibly, the existingsplitfilecould attempt to detect whether the caller intends sync or async support. But providing a separate function seems clearer in any case.
To me, the second approach sounds more interesting and would match exactly what the current splitstream library does. It could also reuse most of the existing code (after some refactoring) boiling down to a more complex state machine for the generator.
I'm available to provide an initial implementation. Although I've not yet worked with the async protocols in C extensions, I've looked at the underlying concepts and am confident to get something working (open questions: how to implement send()/throw()/close() for the iterator representing the awaitable 🤔 ).