An open API service indexing awesome lists of open source software.

https://github.com/brian-goo/rstypes

Thread-safe async-aware types for Python written in Rust
https://github.com/brian-goo/rstypes

async asyncio cache dict python rust thread thread-safe-cache threadsafe

Last synced: 4 months ago
JSON representation

Thread-safe async-aware types for Python written in Rust

Awesome Lists containing this project

README

          

# rstypes

[![CI](https://img.shields.io/github/actions/workflow/status/brian-goo/rstypes/ci.yml?branch=main&logo=github&label=CI)](https://github.com/brian-goo/rstypes/actions?query=event%3Apush+branch%3Amain+workflow%3ACI)
[![pypi](https://img.shields.io/pypi/v/rstypes.svg)](https://pypi.org/project/rstypes)
[![Python versions](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-blue)](https://github.com/brian-goo/rstypes)
[![PyPI Downloads](https://static.pepy.tech/badge/rstypes)](https://pepy.tech/projects/rstypes)

`rstypes` is a minimal Python package providing **thread-safe**, **async-aware** data types implemented in **Rust**. It is designed for high-performance concurrent and asynchronous programming in Python.

This library is ideal for building:
- โœ… **Global or shared dictionaries** across threads or async tasks
- โœ… **In-memory async-aware caches** with TTL support

Internally, each instance uses a single **mutex**, not per-key locks, which keeps the locking semantics simple and predictable.

## Features

- ๐Ÿงต **Thread-safe**: Types that can be safely used across multiple threads.
- โšก **Async-aware**: Compatible with `asyncio` and async/await patterns
- ๐Ÿš€ **Rust-powered**: Built with [PyO3](https://github.com/PyO3/pyo3) for blazing-fast native performance
- ๐Ÿ” **Default value support**: Behaves like `collections.defaultdict`

## Installation

```bash
pip install rstypes
```

## Example Usage

### ๐Ÿ”น Minimal Example

```python
import asyncio

from rstypes import RMap

async def main():
d = RMap()
await d.set(key="hello", value=123)
val = await d.get("hello")
assert val == 123

await d.set(key=7, value="answer")
await d.pop(7)
val = await d.get(7)
assert val is None

asyncio.run(main())
```

### ๐Ÿ”น Async `defaultdict` Behavior

```python
import asyncio

from rstypes import RMap

async def main():
rlock = RMap(asyncio.Lock) # like defaultdict(asyncio.Lock)

async with await rlock.get("mykey"):
print("Inside an asyncio.Lock")

asyncio.run(main())
```

### ๐Ÿ”น Memory Cache with TTL

```python
import asyncio
from typing import NamedTuple

from rstypes import RCacheMap

class Foo(NamedTuple):
name: str
age: int

async def main():
cache = RCacheMap()

await cache.set(key="hello", value=123, ttl=1.0) # 1 second TTL
await cache.set(key="user", value=Foo(name="Alice", age=30), ttl=2.0) # 2 seconds TTL

await cache.pop("hello")
assert await cache.get("hello") is None

user = await cache.get("user")
assert isinstance(user, Foo)

await asyncio.sleep(2.1)
await cache.pop_expired()
assert await cache.get("user") is None

asyncio.run(main())
```

## License

`rstypes` is licensed under the MIT License. See [LICENSE](LICENSE) for more information.