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.1.0.tar.gz (7.6 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.1.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: anyio_ext-0.1.0.tar.gz
  • Upload date:
  • Size: 7.6 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.1.0.tar.gz
Algorithm Hash digest
SHA256 8b0a53aae60f2ccb9e1b43622165640d2f412599e68b1aa6b9be20d6d8513d25
MD5 c8c92a1e05accf34b50dfca61cd230bf
BLAKE2b-256 ec4756fd8147e8e55871d09ce468698ea92986f2c10439a45d5a458cfe7be030

See more details on using hashes here.

File details

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

File metadata

  • Download URL: anyio_ext-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.0 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f84f551582c64a2c55c56a18f981edca0063ea34c1d06d837530041b3c43916d
MD5 9f517eac7f0ad982259e68532973ae63
BLAKE2b-256 261fa7622509d830c0afa18d4e3ed4361d835df91c1f72b29f8b4d5d4eff5da7

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