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
- Host: GitHub
- URL: https://github.com/brian-goo/rstypes
- Owner: brian-goo
- License: mit
- Created: 2025-05-12T09:57:11.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-08-06T14:49:41.000Z (8 months ago)
- Last Synced: 2025-09-29T12:09:54.845Z (6 months ago)
- Topics: async, asyncio, cache, dict, python, rust, thread, thread-safe-cache, threadsafe
- Language: Python
- Homepage:
- Size: 49.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rstypes
[](https://github.com/brian-goo/rstypes/actions?query=event%3Apush+branch%3Amain+workflow%3ACI)
[](https://pypi.org/project/rstypes)
[](https://github.com/brian-goo/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.