Skip to main content

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 benchmarks/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

Note: slice operations on lists are not supported (e.g. state.mylist[1:3]). Use individual index access instead.

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 benchmarks/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
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

persistedstate-26.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

persistedstate-26.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file persistedstate-26.1.tar.gz.

File metadata

  • Download URL: persistedstate-26.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for persistedstate-26.1.tar.gz
Algorithm Hash digest
SHA256 3bc85046bc6996067e4603888b0e2c8b337549f7a3cf8a3f3ae8411fc4e48bad
MD5 f1a7055190b77eae8a6d2582ec45c77d
BLAKE2b-256 55c751c214c147cfbedd2ea34c1ae84f4178a1f19c145c637b8ba136b60736a9

See more details on using hashes here.

File details

Details for the file persistedstate-26.1-py3-none-any.whl.

File metadata

  • Download URL: persistedstate-26.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for persistedstate-26.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6a3fbc2c47284cccb5749b6128698b6cb77e51d84230606c4b5c2dcc4a2e5388
MD5 bf0ef7109485e564f1c7b0ab46f6f675
BLAKE2b-256 35067cb7ae5b01007127360fbe75481ea353347c60e9642343c44f08c30304aa

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