Skip to main content

Transparent persistent state management for Python functions

Project description

StatefulPy

StatefulPy provides transparent, persistent state management for regular Python functions.

Features

  • Simple Decorator API: Add persistent state to any function with a decorator
  • Multiple Backends: Store state in SQLite (embedded) or Redis (distributed)
  • Automatic State Management: State is automatically loaded, saved, and synchronized
  • Concurrency Safe: Locks ensure state consistency across threads and processes
  • Flexible Serialization: Support for pickle, JSON, and custom serializers
  • CLI Tools: Manage, migrate, and monitor your function state

Installation

pip install statefulpy

For Redis support:

pip install statefulpy[redis]

Security Warning:
The default serializer used by StatefulPy is Pickle for performance reasons.
However, Pickle is insecure if processing untrusted input.
For public-facing or untrusted data applications, use the JSON serializer instead:

@stateful(backend="sqlite", db_path="state.db", serializer="json")
def my_function():
    # Your function code...

Quickstart

from statefulpy import stateful

@stateful(backend="sqlite", db_path="counter.db")
def counter():
    # Initialize state if needed
    if "count" not in counter.state:
        counter.state["count"] = 0
    
    # Update state
    counter.state["count"] += 1
    
    return counter.state["count"]

# The counter value persists across runs
print(counter())  # 1 (first run)
print(counter())  # 2
# Restart your program...
print(counter())  # 3 (value was loaded from storage)

Backend Options

SQLite (Default)

@stateful(backend="sqlite", db_path="state.db", serializer="pickle")
def my_function():
    # Your function code...

Redis

@stateful(backend="redis", redis_url="redis://localhost:6379/0")
def my_function():
    # Your function code...

Global Configuration

from statefulpy import set_backend

# Set default backend for all @stateful functions
set_backend("redis", redis_url="redis://localhost:6379/0")

Serialization

StatefulPy supports multiple serialization formats:

  • Using JSON serializer:
    For security, the recommended serializer for public-facing applications is JSON.

    @stateful(backend="sqlite", db_path="state.db", serializer="json")
    def my_function():
        # Your function code...
    
  • Using the Pickle serializer:
    Pickle offers better performance but is insecure against untrusted input.

    ⚠️ Warning: Only use Pickle when handling trusted data.

    @stateful(backend="sqlite", db_path="state.db", serializer="pickle")
    def my_function():
        # Your function code...
    

Command-Line Interface

Initialize storage:

statefulpy init --backend sqlite --path state.db

Migrate between backends:

statefulpy migrate --from sqlite --to redis --from-path state.db --to-path redis://localhost:6379/0

Check backend health:

statefulpy healthcheck --backend redis --path redis://localhost:6379/0

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

statefulpy-0.1.1.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

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

statefulpy-0.1.1-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file statefulpy-0.1.1.tar.gz.

File metadata

  • Download URL: statefulpy-0.1.1.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for statefulpy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 580a04d37e55f878ef80eb2ad1b7e2d6d075950120b263e6051bf09908c34060
MD5 000ee07dcda8c5ed45ec20cd456da336
BLAKE2b-256 c9f4042e96e7d96897898283eca187ff902423cc0d1eed854cc0619aa7995e97

See more details on using hashes here.

File details

Details for the file statefulpy-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: statefulpy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for statefulpy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 25b4626556a2042c137726eb1b38de321ef649a5d01708b850c6905bd926195c
MD5 2a64ebf43c1a61461034d0f90cd4c58b
BLAKE2b-256 3ebdce21e52014d246558337ea15636bea983df816082c325f94d1faeee44770

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