Python package for distributed state
Project description
Distributed Redis State Model
A lightweight Pydantic-based base class for keeping your model state in sync across multiple Python services via Redis.
Each instance is identified by a unique id_ and will automatically read/write its fields to Redis whenever they change.
Features
- Pydantic model: full data‐validation, parsing and type hints.
- Automatic Redis sync: reads from Redis on init and writes back on assignment.
- Portable: move instances between services (same class name +
id_) and state stays in sync. - Minimal dependencies: just
pydanticandredis-py.
Installation
pip install pydantic-state-manager
Quickstart
from src.base_redis_state_model import BaseRedisStateModel
class DistributedTaskState(BaseRedisStateModel):
"""
Tracks the lifecycle state of a distributed task.
"""
current_state: str = "scheduled"
# Create or load the state for “task_1”
task_state = DistributedTaskState(id_="task_1")
# Update the state; this writes immediately to Redis
task_state.current_state = "running"
# In another service/process (with same code):
# state = DistributedTaskState(id_="task_1")
# print(state.current_state) # → "running"
task_state.current_state = "completed"
How It Works
-
Initialization On
__init__, the model attempts to fetch any existing JSON blob from Redis at key"{ClassName}:{id_}". If found, it populates the model fields from that JSON; otherwise it uses the default values you defined in your subclass. -
Attribute Assignment Whenever you set any field on your model, the base class overrides
__setattr__to:- Validate the new value (via Pydantic)
- Write it back to Redis under the same key
-
Cross-Service Sync As long as each service imports the same subclass name and gives it the same
id_, any assignment in one process will be immediately visible to others when they next access the property (or reinstantiate the object).
API Reference
class BaseRedisStateModel(pydantic.BaseModel)
| Parameter | Type | Required | Description |
|---|---|---|---|
id_ |
str |
Yes | Unique identifier for this model instance. |
Configuration
Under the hood, the Redis connection is managed via environment variables (or defaults):
STATE_MANAGER_REDIS_URL(default:redis://localhost:6379/0)
You can override these at import time:
import os
os.environ["STATE_MANAGER_REDIS_URL"] = "redis-prod.mycompany.internal"
License
MIT © 2025 Ruslan Schalkenbajew
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_state-0.0.7.tar.gz.
File metadata
- Download URL: pydantic_state-0.0.7.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a684a47dc379cc1e270562205bc9a922ad12bcc8ecc6d72bb24ac29b35a8ea7e
|
|
| MD5 |
8fff15e83172e75a56e55da3aa97d832
|
|
| BLAKE2b-256 |
da2c8b9caeb0d260eaabd9f03e05a67a8f9e82418c6603c65c21ae615128c344
|
File details
Details for the file pydantic_state-0.0.7-py3-none-any.whl.
File metadata
- Download URL: pydantic_state-0.0.7-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4565f43540565c8b03e37c819e4749b4f9811125b944f6f9ab70d5fdda89edc4
|
|
| MD5 |
5f8cd5c1bd5e26773f813f77824795d8
|
|
| BLAKE2b-256 |
56221b930591f48beedfe144ec763169ecc0461c02d8e7205a97cb0f349742d0
|