Skip to main content

asyncio Channels (closable queues) inspired by golang

Project description

aiochannel - AsyncIO Channel

Build Status Stable Version Python Versions Download Stats

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():
    # A Channel takes a max queue size and an loop
    # both optional. loop is not recommended as
    # in asyncio is phasing out explicitly passed event-loop
    my_channel: Channel[str] = Channel(100)

    # You add items to the channel with
    await my_channel.put("my item")
    # Note that this can throw ChannelClosed if the channel
    # is closed, during the attempt at adding the item
    # to the channel. Also note that .put() will block until
    # it can successfully add the item.


    # Retrieving is done with
    my_item = await my_channel.get()
    # Note that this can also throw ChannelClosed if the
    # channel is closed before or during retrival.
    # .get() will block until an item can be retrieved.

    # Note that this requires someone else to close and drain
    # the channel.
    # Lastly, you can close a channel with `my_channel.close()`
    # In this example, the event-loop call this asynchronously
    asyncio.get_event_loop().call_later(0.1, my_channel.close)

    # You can wait for the channel to be closed and drained:
    await my_channel.join()

    # Every call to .put() after .close() will fail with
    # a ChannelClosed.
    # you can check if a channel is marked for closing with
    if my_channel.closed():
        print ("Channel is closed")

asyncio.run(main())

Like the asyncio.Queue you can also call non-async get and put:

    # non-async version of put
    my_channel.put_nowait(item)
    # This either returns None,
    # or raises ChannelClosed or ChannelFull

    # non-async version of get
    my_channel.get_nowait()
    # This either returns the next item from the channel
    # or raises ChannelEmpty or ChannelClosed
    # (Note that ChannelClosed emplies that the channel
    # is empty, but also that is will never fill again)

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.

    # the channel might contain data here
    async for item in channel:
        print(item)
    # the channel is closed and empty here

which is functionally equivalent to

    while True:
        try:
            data = yield from channel.get()
        except ChannelClosed:
            break
        # process data here

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiochannel-1.4.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiochannel-1.4.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file aiochannel-1.4.1.tar.gz.

File metadata

  • Download URL: aiochannel-1.4.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.13.5 Darwin/25.5.0

File hashes

Hashes for aiochannel-1.4.1.tar.gz
Algorithm Hash digest
SHA256 a2c43c83f27c995669b2ef3d4c99abddd6ceec26d8170c4c8d04ab125c4227de
MD5 fa5a64b5d623f2ebfb2541990f88b62f
BLAKE2b-256 b344939ac49ea3bc585cf21a276a3cd078f3c1c54c5ad2b0f4dc58abc683475b

See more details on using hashes here.

File details

Details for the file aiochannel-1.4.1-py3-none-any.whl.

File metadata

  • Download URL: aiochannel-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.13.5 Darwin/25.5.0

File hashes

Hashes for aiochannel-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eb0c90168f2eb8f0193ec73c5cdd06e38e4ae856e07b2303006c1b489d533c31
MD5 b51f00cbb9908320f435f887990ca367
BLAKE2b-256 9d736b085596497843a6912c15793aa9b97109d54f8ce845d18121cc9b4101c5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page