anyio-ext
A collection of helpful utilities for anyio.
Features
- Powerful, easy-to-use caching decorator
- Typed
asyncio.gatherimplementation - Generic
QueueandStackprimitives
Installation
$ pip install anyio-ext
Getting started
gather() is similar to asyncio.gather():
from anyio import sleep
from anyio_ext import gather
results = await gather(*[sleep(i) for i in range(3)])
print(results) # (None, None, None)
Caching is implemented with a decorator:
from anyio_ext import cached
@cached(ttl=60)
async def my_task() -> int: ...
Queues
Queue and stack implementations work similarly:
from anyio_ext import Queue, Stack
queue = Queue[int](max_size=32)
stack = Stack[int](max_size=32)
await queue.push(1)
await queue.push(2)
print(await queue.pop()) # 1
await stack.push(1)
await stack.push(2)
print(await stack.pop()) # 2
Advanced caching
Cache keys are generated by hashing arguments. You can exclude non-serializable arguments from cache key construction:
from sqlalchemy.ext.asyncio import AsyncSession
@cached(ttl=60, exclude={"session"})
async def my_task(session: AsyncSession) -> int: ...
You can also customize which parts of arguments get hashed:
@cached(
ttl=60,
key_fns={
# hash just the ID, not the entire model
"user": lambda u: u.id,
# hash a couple relevant fields
"message": lambda m: (m.type, m.timestamp),
},
)
async def my_task(user: User, message: Message) -> int: ...
You can easily invalidate keys by passing the same arguments:
@cached(ttl=60)
async def my_task(time: int) -> int: ...
await my_task.invalidate(3)
Simple cache statistics are available:
print(my_task.hits, my_task.misses, len(my_task)) # 999 1 1
Methods can also be cached:
class MyClass:
@cached(ttl=60)
async def my_task(self, time: int) -> int: ...
instance = MyClass()
await instance.my_task(3)
instance.my_task.invalidate(3)
print(MyClass.my_task.hits, MyClass.my_task.misses, len(MyClass.my_task)) # 0 1 0
Note that statistics are on a per-class basis, but calls to my_task() are on a per-instance basis (even though behind the scenes, the cache is shared across all instances). self is one of the parameters used for caching by default, but it won't cause memory leaks as keys don't store references to arguments.
Metadata
Release files for anyio-ext 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| anyio_ext-0.2.0.tar.gz | 7.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| anyio_ext-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.0 kB
Release files / anyio_ext-0.2.0.tar.gz
| Download URL | anyio_ext-0.2.0.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
aa525edbba825a010af0dc812fe986b158242df3ef782a6329e0237dbff27c66
|
|
BLAKE2b-256 checksum How to use checksums |
7ddc617e565a5ca4021dc67ba5c102b7ef6cbe90c60015bc74902dc0a9779c72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / anyio_ext-0.2.0-py3-none-any.whl
| Download URL | anyio_ext-0.2.0-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fb9f1dd56047de726feda72d00531e022461e5ac5aa44f956e6e398c68b4bb8d
|
|
BLAKE2b-256 checksum How to use checksums |
686e637ded861a9a3a288d4ff3cd0181feba47f965bbb911a6f7f1689cba8772
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|