Channels between coroutines
Project description
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
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
Built Distribution
File details
Details for the file sav.channels-0.5.tar.gz
.
File metadata
- Download URL: sav.channels-0.5.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aaffa3baefd3821d44b77916febbedc44b78c010f0f9c5f409955bce55134ee3 |
|
MD5 | d0d487cf3929433553016e26b2d5386f |
|
BLAKE2b-256 | 7d6c54f2c1f586f85496c80542f443f8ce121d460c61b3bccf8b9d1ad61b8873 |
File details
Details for the file sav.channels-0.5-py3-none-any.whl
.
File metadata
- Download URL: sav.channels-0.5-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88791a924b0db964362b9671d0b7251ff52fc36d5c830b2d15e7038b7fdc7e1a |
|
MD5 | 33d7c3d8776d887fd18f37ce1d78f6bf |
|
BLAKE2b-256 | 190d80670b06b7ba62235dce58eeb120820f03945e6ea7a29929ec3e5e630c70 |