Skip to main content

Channels between coroutines

A simple Python implementation of the channel synchronization construct for async/await applications.

Channels are used for synchronization in the CSP concurrency model. They are natively supported by languages that implement this model, such as occam and Go. Python has asynchronous generators, which are similar to channels except that they require yielding instead of calling from one of the two endpoints. While this makes no difference in many cases, some problems are easier to solve if a data stream can be accessed from both ends by calling instead of yielding.

The sav.channels module implements channels as pairs of asynchronous generators. When an object is sent into one generator, it will be yielded by the other generator, and vice-versa.

Installation

This module requires Python 3.8 or higher. Use pip to install it from the command line:

pip install sav.channels

Or visit the online project pages on GitHub and PyPI.

Example

import asyncio
from sav import channels

a_receiver, a_sender = channels.create()
b_receiver, b_sender = channels.create()

async def send_messages():
    """Send messages into multiple channels."""
    async with channels.open(a_sender), channels.open(b_sender):
        await a_sender.asend('Hello Arnold.')
        await b_sender.asend('Hello Bernard.')
        await a_sender.asend('Goodbye Arnold.')
        await b_sender.asend('Goodbye Bernard.')

async def show_messages(name, receiver):
    """Show messages from a single channel."""
    async for message in receiver:
        print(f'Message for {name}: {message}')    

async def main():
    """Run both channels concurrently."""
    await asyncio.gather(send_messages(),
                         show_messages('Arnold', a_receiver),
                         show_messages('Bernard', b_receiver))

asyncio.run(main())

Documentation

See the documentation for further details.

Or use Sphinx to build a local copy of the documentation from the package source:

cd docs
make singlehtml

Release files for sav.channels 0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sav.channels 0.5
File Size Uploaded
sav.channels-0.5.tar.gz 3.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sav.channels 0.5
File Interpreter ABI Platform
sav.channels-0.5-py3-none-any.whl Python 3 none any Details

Total release size: 8.1 kB

Release files / sav.channels-0.5.tar.gz

Download URL sav.channels-0.5.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
aaffa3baefd3821d44b77916febbedc44b78c010f0f9c5f409955bce55134ee3
BLAKE2b-256 checksum
How to use checksums
7d6c54f2c1f586f85496c80542f443f8ce121d460c61b3bccf8b9d1ad61b8873
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.8.0

Release files / sav.channels-0.5-py3-none-any.whl

Download URL sav.channels-0.5-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
88791a924b0db964362b9671d0b7251ff52fc36d5c830b2d15e7038b7fdc7e1a
BLAKE2b-256 checksum
How to use checksums
190d80670b06b7ba62235dce58eeb120820f03945e6ea7a29929ec3e5e630c70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.8.0

Release history Release notifications | RSS feed

This release

0.5 This release

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page