Skip to main content

A collection of helpful utilities for anyio.

Project description

PyPI Downloads Release

anyio-ext

A collection of helpful utilities for anyio.

Features

  • Powerful, easy-to-use caching decorator
  • Typed asyncio.gather implementation
  • Generic Queue and Stack primitives

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.

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

anyio_ext-0.2.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anyio_ext-0.2.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file anyio_ext-0.2.0.tar.gz.

File metadata

  • Download URL: anyio_ext-0.2.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for anyio_ext-0.2.0.tar.gz
Algorithm Hash digest
SHA256 aa525edbba825a010af0dc812fe986b158242df3ef782a6329e0237dbff27c66
MD5 e62036dcae9df1deaff926c9bf5e2246
BLAKE2b-256 7ddc617e565a5ca4021dc67ba5c102b7ef6cbe90c60015bc74902dc0a9779c72

See more details on using hashes here.

File details

Details for the file anyio_ext-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: anyio_ext-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for anyio_ext-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb9f1dd56047de726feda72d00531e022461e5ac5aa44f956e6e398c68b4bb8d
MD5 6d7949dc6e3d65be32dc07f8093175d6
BLAKE2b-256 686e637ded861a9a3a288d4ff3cd0181feba47f965bbb911a6f7f1689cba8772

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page