Ultra fast persistent store for small states in mapped YAML file
Project description
PersistedState
Simple and fast solution for persisting small states:
from persistedstate import PersistedState
STATE = PersistedState("example.state", last_id=0)
for current_id in range(STATE.last_id + 1, 100_001):
print(f"Processing item #{current_id}")
STATE.last_id = current_id
print("Processing DONE.")
You can interrupt this script, and next time it will continue from the first unprocessed item. As another example see perftest.py.
Mapped YAML state file
The use case is persisting small amount of data which can be edited easily with a text editor. The database is an UTF-8 encoded YAML file, so it can be highlighted and edited manually if needed. (It also uses YAML stream file format for journal.) The YAML file is fully mapped to Python objects, so every change is synchronized to disk immediately. E.g. you can do this:
STATE = PersistedState("state.yaml", processed_items=[])
STATE.setdefault("key", {})
STATE["key"].setdefault("nested", 2)
STATE.processed_items.append("<some item>")
STATE["key"]["nested"] += 1
Failure tolerance
It uses Write-Ahead-Logging and atomic vacuum, so there will be no data loss.
Thread safe
Changing the state is thread safe. You also can use the ._thread_lock
attribute to make atomic changes:
def atomic_increment():
with STATE._thread_lock:
STATE.counter += 1
Performance
For its use case it outperforms existing key-value store modules. For example incrementing a counter (for the details see perftest.py):
state.counter = 0
for _ in range(COUNT_TO):
state.counter += 1
Counting to 10,000 (using Windows and Python 3.11):
PersistedState 0.193 sec
DiskCache 1.222 sec
SqliteDict 3.089 sec
PickleDb 8.251 sec
Lmdb 14.944 sec
Shelve 39.771 sec
The example seems to be silly, but this is very close to the use case it was developed for. For complex data structures or big amount of data I suggest using other libraries, like DiskCache. (The rule of thumb is when your state file is too big to be edited easily in your favorite text editor, you may think about using another key-value store library.)
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
Built Distribution
File details
Details for the file persistedstate-24.2.tar.gz
.
File metadata
- Download URL: persistedstate-24.2.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d41e24319caa66061c2b15de343b5f5c2cc74010ac9113cbfc4b936f3ff0c15a |
|
MD5 | cf45bcc8951067b6abab7f9f1a1f0f5e |
|
BLAKE2b-256 | efdf31b62edb3d5f49766e6c3805454e0eebf233964e03307d6a0a2ef883cd9a |
File details
Details for the file persistedstate-24.2-py3-none-any.whl
.
File metadata
- Download URL: persistedstate-24.2-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5dfd6a912b265ac30a3ca28fde1f889577683ae9c76312c532a5ca1b6bcdf59 |
|
MD5 | 76034758a7b3a3de800ee91bb1dbe67a |
|
BLAKE2b-256 | 1836deb7d75e299db344521ce4ad13c27d185ef0fa6a160d9e8c5c8480275be1 |