Skip to main content

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 pydantic and redis-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

  1. 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.

  2. 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
  3. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_state-0.0.7.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_state-0.0.7-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

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

Hashes for pydantic_state-0.0.7.tar.gz
Algorithm Hash digest
SHA256 a684a47dc379cc1e270562205bc9a922ad12bcc8ecc6d72bb24ac29b35a8ea7e
MD5 8fff15e83172e75a56e55da3aa97d832
BLAKE2b-256 da2c8b9caeb0d260eaabd9f03e05a67a8f9e82418c6603c65c21ae615128c344

See more details on using hashes here.

File details

Details for the file pydantic_state-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_state-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 4565f43540565c8b03e37c819e4749b4f9811125b944f6f9ab70d5fdda89edc4
MD5 5f8cd5c1bd5e26773f813f77824795d8
BLAKE2b-256 56221b930591f48beedfe144ec763169ecc0461c02d8e7205a97cb0f349742d0

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