The simple solution for sharing async data streams in Python.
pip install streamx
from streamx import AsyncStream
stream = AsyncStream[int]()
You can push items into a stream using the push method. This method is a coroutine, so you'll need to await it. All listening tasks will receive each item.
await stream.push(1)
await stream.push(2)
await stream.push(3)
To consume a stream, you can use the listen method. This method returns an async iterator, so you can use it with an async for loop. Many tasks can listen to the same stream at the same time, and each task will receive each item pushed into the stream while it is listening.
with stream.listen() as listener:
async for item in listener:
print(item)
Once you're done pushing data into a stream, you should close it to signal to consumers that there will be no more data. This signals to exit the async for loop, and prevents any new consumers from listening to the stream.
await stream.close()
import asyncio
from streamx import AsyncStream
async def producer(stream: AsyncStream[int]):
for i in range(5):
await stream.push(i)
await asyncio.sleep(1)
await stream.close()
async def listener(stream: AsyncStream[int]):
with stream.listen() as listener:
async for item in listener:
print(item)
async def main():
stream = AsyncStream[int]()
await asyncio.gather(producer(stream), listener(stream), listener(stream))
asyncio.run(main())