Some (maybe) useful extensions for asyncio
Project description
asyncio-extensions
Installation
pip install asyncio-extensions
Usage
TaskGroup
asyncio-extensions provides a cancellable version of AsyncIO's TaskGroup.
import asyncio
from asyncio_extensions import TaskGroup
queue = asyncio.Queue()
async with TaskGroup() as tg:
for _ in range(10):
tg.create_task(consume_from_queue(queue))
await add_to_queue(queue)
await queue.join()
tg.cancel()
LimitedTaskGroup
A version of TaskGroup that limits the number of concurrently running tasks.
import asyncio
from asyncio_extensions import LimitedTaskGroup
queue = asyncio.Queue()
async with LimitedTaskGroup(3) as tg:
for _ in range(50):
tg.create_task(some_expensive_operation(queue))
await add_to_queue(queue)
await queue.join()
tg.cancel()
checkpoint
The checkpoint function yields control to the event loop. It is a more elegant approach to do-nothing tasks, giving other tasks a chance to run.
from asyncio_extensions import checkpoint
class DummyChannel:
async def send_message(self, message):
await checkpoint()
sleep_forever
The sleep_forever function never returns. It simply keeps yielding control to the event loop.
from asyncio_extensions import sleep_forever
class DummyChannel:
async def receive_message(self):
await sleep_forever()
heartbeat
The heartbeat function runs a given callable at a regular interval.
from asyncio_extensions import heartbeat
async def ping():
pass
async with TaskGroup() as tg:
tg.create_task(heartbeat(5, ping))
await some_long_running_process()
asyncify
The asyncify function ensures a callable can be awaited. If the callable is already a coroutine function, it is returned as-is. Otherwise, it is wrapped so that calls run in a separate thread.
from asyncio_extensions import asyncify
def blocking_read(path: str) -> str:
with open(path) as f:
return f.read()
async def main():
content = await asyncify(blocking_read)("data.txt")
It can also be used as a decorator:
from asyncio_extensions import asyncify
@asyncify
def blocking_read(path: str) -> str:
with open(path) as f:
return f.read()
async def main():
content = await blocking_read("data.txt")
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
asyncio-extensions is distributed under the terms of the MIT license.
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
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 asyncio_extensions-0.0.4.tar.gz.
File metadata
- Download URL: asyncio_extensions-0.0.4.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfc1be3f939e0d897e8fee585ef2bd5226f1327fe5500cb49ab943f35fdf3247
|
|
| MD5 |
0689b1a6ffa63d831c9b6f2d8f0cb0d5
|
|
| BLAKE2b-256 |
4157aeff36eef91781a332d510d70e6c397de2b605430064bbd149f0af43284e
|
Provenance
The following attestation bundles were made for asyncio_extensions-0.0.4.tar.gz:
Publisher:
release.yml on hartungstenio/asyncio-extensions
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asyncio_extensions-0.0.4.tar.gz -
Subject digest:
bfc1be3f939e0d897e8fee585ef2bd5226f1327fe5500cb49ab943f35fdf3247 - Sigstore transparency entry: 1125792604
- Sigstore integration time:
-
Permalink:
hartungstenio/asyncio-extensions@7cf2257f89fba89044bbe49ea7e51aff892411a6 -
Branch / Tag:
refs/tags/0.0.4 - Owner: https://github.com/hartungstenio
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7cf2257f89fba89044bbe49ea7e51aff892411a6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file asyncio_extensions-0.0.4-py3-none-any.whl.
File metadata
- Download URL: asyncio_extensions-0.0.4-py3-none-any.whl
- Upload date:
- Size: 6.8 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 |
69931da2224e816de0a75d2583368a946d4871e5b5d99528fcd2c34856e424d9
|
|
| MD5 |
ee3870598835c4fc3b5039695c980898
|
|
| BLAKE2b-256 |
42bcb263534b9336c36180858d235977fe9b74449e2f71d2dcca607457ac8816
|
Provenance
The following attestation bundles were made for asyncio_extensions-0.0.4-py3-none-any.whl:
Publisher:
release.yml on hartungstenio/asyncio-extensions
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asyncio_extensions-0.0.4-py3-none-any.whl -
Subject digest:
69931da2224e816de0a75d2583368a946d4871e5b5d99528fcd2c34856e424d9 - Sigstore transparency entry: 1125792670
- Sigstore integration time:
-
Permalink:
hartungstenio/asyncio-extensions@7cf2257f89fba89044bbe49ea7e51aff892411a6 -
Branch / Tag:
refs/tags/0.0.4 - Owner: https://github.com/hartungstenio
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7cf2257f89fba89044bbe49ea7e51aff892411a6 -
Trigger Event:
release
-
Statement type: