py-key-value-aio is an asynchronous Python library offering a pluggable interface for various Key-Value (KV) stores. It provides a backend-agnostic abstraction layer, allowing framework authors and applications to integrate KV storage without committing to a specific implementation like Redis, DynamoDB, or an in-memory solution. The library, currently at version 0.4.4, focuses exclusively on async/await patterns and requires Python >=3.10. It is actively maintained as part of a larger monorepo.
pip install py-key-value-aioVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the core `put`, `get`, and `delete` operations using the `AsyncKeyValue` protocol and an in-memory store. It highlights the asynchronous nature of the library and the common pattern of accepting the `AsyncKeyValue` protocol in your application logic, allowing the underlying store implementation to be swapped easily.
Rewrite blocking code paths to use `async/await` and integrate with an `asyncio` event loop. Do not attempt to use this library in synchronous contexts directly.
Always assume retrieved values are copies. If modifications need to be persisted, explicitly call `await key_value_store.put(key, updated_value, ...)` after modifying the local copy.
Ensure that values passed to `put` and retrieved data conform to the expected types, especially when using Pydantic models. For debugging or specific scenarios, runtime type checks can be disabled by setting the environment variable `PY_KEY_VALUE_DISABLE_BEARTYPE=true`.
Always consult the official `py-key-value` GitHub monorepo documentation for details on individual backend implementations (e.g., DynamoDB, S3, Redis) before deploying them in production environments.
Ensure your project's Python interpreter is version 3.10 or newer before attempting to install and use `py-key-value-aio`.
Install the library using pip: `pip install py-key-value-aio`.
Ensure your `py-key-value-aio` version is compatible with the code you are using (e.g., `pip install --upgrade py-key-value-aio` to get the latest), and consult the official documentation for the correct import paths for your version.
Prepend the call to the asynchronous function with `await`. For example, change `store.get(...)` to `await store.get(...)`.
If the coroutine should run, either `await` it, `yield from` it, or schedule it as a task, e.g., `asyncio.create_task(my_coroutine())`. If it's the entry point, use `asyncio.run(my_coroutine())`.
Before accessing a key, check for its existence (e.g., `if key in await store.keys():`) or use methods that provide a default value for missing keys (e.g., `value = await store.get(key, default=None)`), or implement a `try-except KeyError` block to handle the missing key gracefully.