pychanasync is a lightweight python package which brings Go-style channels to python's asyncio concurrency world. It is an async-channel implementation, providing a channel shaped tool for channel shaped problems.
Project description
Pychanasync
pychanasync is a lightweight python package which brings Go-style channels to
python's asyncio concurrency world. It is an async-channel implementation,
providing a channel shaped tool for channel shaped problems.
pychanasync is implemented entirely around pythons asyncio event loop.
The implementation is lock free ,taking advantage of the single threaded
cooperative concurrency model.
It is designed and implemented to work with coroutines and not threads, providing safe and deterministic communication patterns without blocking the event loop.
Visit documentation site for more details.
Features
- Buffered and unbuffered channel semantics - use either synchronous or buffered communication
- Async iteration over channels - Consume messages from a channel using
async forloops. - Context manager support - close channels and release resources when done with
async with. - Blocking/ awaitable operations -
await chan.push(value)andawait chan.pull()for safe, cooperative communication. - Non-blocking operations -
chan.push_nowait(value)andchan.pull_nowait()for buffered channels when you don’t want to suspend. - Select-like utility - wait on multiple channel operations concurrently, similar to Go’s select statement, in a clean and Pythonic way
Installation
pychanasync is available on PyPi
pip install pychanasync
Quickstart
Channels can be both buffered and unbuffered.
unbuffered channels have no internal buffer capacity. What this means is
every producer (push) will block/suspend until there is a ready consumer on
the other end of the channel (pull) and every consumer until there is a
ready producer on the other end of the channel.
from pychanasync import channel
#create unbuffered channel
ch = Channel()
# send
async ch.push("item") #blocks here
# receive
value = async ch.pull()
buffered channels have an internal buffer capacity and can hold (N)
number of items at a time. When doing a push into a buffered channel, the
operation will only block when the buffer is full and until there is available
space to send the new item. Other than that the operation completes
and returns quickly.
Below is a buffered channel that can hold 300 items at a time.
from pychanasync import channel
ch = Channel(buffer=300)
# send
async ch.push("item")
# receive
value = async ch.pull()
Async Iteration
pychanasync supports async iteration, allowing you to consume items from a channel
in a clean way using async for loop.
We can rewrite our consumer above as
async def consumer(ch):
async for msg in ch:
print(f"Received: {msg}")
Once the producer closes the channel , the iteration ends .
Context manager support
pychanasync has support for asynchronous context managers for automatic cleanup.
We can rewrite out producer component as
async def producer(channel):
async with channel as ch:
for i in range(3):
await ch.push(f"msg {i}")
print(f"Sent msg {i}")
When the async-with block exits , the channel is closed automatically.
Chanselect
The chanselect utility method allows you to start and wait on multiple channel operations simultaneously,
returning the one that completes first.
synthax
chan, value = await chanselect(
(chan_a, chan_a.pull()),
(chan_b, chan_b.pull())
)
checkout more features
Basic consumer-producer example
import asyncio
from pychanasync import Channel
async def producer(ch):
for i in range(3):
await ch.push(f"msg {i}")
print(f"Sent msg {i}")
ch.close() # gracefully close when done
async def consumer(ch):
while True:
try:
msg = await ch.pull()
print(f"Received {msg}")
except Channel.Closed:
break
async def main():
ch = Channel(buffer=2)
await asyncio.gather(producer(ch), consumer(ch))
asyncio.run(main())
Contributing
To contribute or set up the project locally.
find the project source code on github
Clone the project
git clone https://github.com/Gwali-1/PY_CHANNELS_ASYNC
cd PY_CHANNELS_ASYNC
Install dependencies
pipenv install --dev
Running tests From the project root
pipenv run pytest
Installing the package locally From the project root
pip install -e .
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pychanasync-1.0.0.tar.gz.
File metadata
- Download URL: pychanasync-1.0.0.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c63a2515f09241393bf1a32475022bb6d9f34ed82b27737d0123f5568ab9b60f
|
|
| MD5 |
7fc839a365d93a590ace483749b7499e
|
|
| BLAKE2b-256 |
7e4109be1df3be55c0d547273e0b4b947d5fed7bd891334284c6a72b2454a29f
|
Provenance
The following attestation bundles were made for pychanasync-1.0.0.tar.gz:
Publisher:
python-publish.yml on Gwali-1/PY_CHANNELS_ASYNC
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pychanasync-1.0.0.tar.gz -
Subject digest:
c63a2515f09241393bf1a32475022bb6d9f34ed82b27737d0123f5568ab9b60f - Sigstore transparency entry: 628789880
- Sigstore integration time:
-
Permalink:
Gwali-1/PY_CHANNELS_ASYNC@e1c6f4b069a29ee8602257d13d02af1215340479 -
Branch / Tag:
refs/tags/V1.0.0 - Owner: https://github.com/Gwali-1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@e1c6f4b069a29ee8602257d13d02af1215340479 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pychanasync-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pychanasync-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11ef3c501a9fe3e2708bcf47476291f4655d1cf74262d9ba402f0369123d80ab
|
|
| MD5 |
a9ce6927dd624ee7f23171c7709a8706
|
|
| BLAKE2b-256 |
4a9b6fe8397ea7d8c86f7b3da16f2008d219d98359449811c3f25b4bf5376e54
|
Provenance
The following attestation bundles were made for pychanasync-1.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on Gwali-1/PY_CHANNELS_ASYNC
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pychanasync-1.0.0-py3-none-any.whl -
Subject digest:
11ef3c501a9fe3e2708bcf47476291f4655d1cf74262d9ba402f0369123d80ab - Sigstore transparency entry: 628789887
- Sigstore integration time:
-
Permalink:
Gwali-1/PY_CHANNELS_ASYNC@e1c6f4b069a29ee8602257d13d02af1215340479 -
Branch / Tag:
refs/tags/V1.0.0 - Owner: https://github.com/Gwali-1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@e1c6f4b069a29ee8602257d13d02af1215340479 -
Trigger Event:
release
-
Statement type: