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. #\x00 \x00s\x00t\x00a\x00t\x00e\x00f\x00u\x00l\x00p\x00y\x00 \x00 \x00
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 statefulpy-0.1.2.tar.gz.
File metadata
- Download URL: statefulpy-0.1.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11e5e26b6a68953e2f17e868a54c328a8ea86fac84689bb7ec2df2400a9f37fe
|
|
| MD5 |
79776fd8b8261c43e06fc34d2603c6f8
|
|
| BLAKE2b-256 |
e2725af94a7355018ecb80f13b8bc6ecc621fef978381c322f3afed3379e26eb
|
File details
Details for the file statefulpy-0.1.2-py3-none-any.whl.
File metadata
- Download URL: statefulpy-0.1.2-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e510b45bec13943e51bf47a3ef8f9f330d89c3c656d195dbba0f4409a0041c95
|
|
| MD5 |
c84166b424378e6e56ae57e44a1a8979
|
|
| BLAKE2b-256 |
9185fe2d52c9f2eb4fa4a8bacd0d2dac5f6ba0e93a080e16090bf555963ae9ed
|