aiochannel - AsyncIO Channel

Channel concept for asyncio.
Install
pip install aiochannel
Changelog
Changelog
Usage
Basics
Channel has a very similar API to asyncio.Queue.
The key difference is that a channel is only considered
"done" when it has been both closed and drained, so calling .join()
on a channel will wait for it to be both closed and drained (Unlike
Queue which will return from .join() once the queue is empty).
NOTE: Closing a channel is permanent. You cannot open it again.
import asyncio
from aiochannel import Channel
async def main():
my_channel: Channel[str] = Channel(100)
await my_channel.put("my item")
my_item = await my_channel.get()
asyncio.get_event_loop().call_later(0.1, my_channel.close)
await my_channel.join()
if my_channel.closed():
print ("Channel is closed")
asyncio.run(main())
Like the asyncio.Queue you can also call non-async get and put:
my_channel.put_nowait(item)
my_channel.get_nowait()
As of 0.2.0 Channel also implements the async iterator protocol.
You can now use async for to iterate over the channel until it
closes, without having to deal with ChannelClosed exceptions.
async for item in channel:
print(item)
which is functionally equivalent to
while True:
try:
data = yield from channel.get()
except ChannelClosed:
break