Skip to main content

spork-state

Tests PyPI Python

Thread-safe state management with one implementation for both Spork and Python.

spork-state provides Atom: a mutable reference whose updates are synchronized, validated before commit, and observable through synchronous watches. The implementation and type declarations live in Spork; spork build generates idiomatic package-level Spork and typed Python APIs directly from the manifest.

Install

Python projects:

python -m pip install spork-state

Spork projects add the package to spork.it and synchronize the project environment:

:dependencies ["spork-state>=0.2,<0.3"]
spork sync

spork-state supports Python 3.10–3.14, including free-threaded 3.14.

Python API

from spork_state import Atom

counter = Atom(0, validator=lambda value: value >= 0)

unsubscribe = counter.subscribe(
    lambda old, new: print(f"{old} -> {new}"),
    fire_immediately=True,
)

counter.swap(lambda value, amount: value + amount, 3)
assert counter.value == 3
assert counter.compare_and_set(counter.value, 4)

unsubscribe()

Functional equivalents (atom, deref, swap, reset, and others) are also exported.

Spork API

(ns example.counter
  (:require [spork-state :as state]))

(def counter (state.atom 0 (fn [value] (>= value 0))))

(state.add-watch! counter :log
  (fn [key reference old-value new-value]
    (print old-value "->" new-value)))

(state.swap! counter (fn [value amount] (+ value amount)) 3)
(assert (= (state.deref counter) 3))

The core Spork functions are atom, atom?, deref, swap!, swap-vals!, reset!, reset-vals!, compare-and-set!, add-watch!, remove-watch!, get-validator, and set-validator!.

Guarantees

  • swap, reset, validator replacement, and compare-and-set are linearizable.
  • A swap function runs exactly once while the atom's reentrant lock is held.
  • Validators run before commit. Rejection leaves the old value unchanged.
  • Watches run synchronously after commit, outside the lock, in registration order.
  • A watch exception propagates but never rolls back committed state.
  • Compare-and-set and change notification use object identity, not equality.
  • Reading an atom is safe; mutating a mutable value obtained from it is not synchronized. Prefer immutable values.

See the API reference and design semantics for concurrency details.

Development

Development requires spork-lang 0.5.x, and release distributions are built and tested with 0.5.0. Published packages depend only on spork-runtime, so Python consumers do not install the compiler. The public Spork namespace, Python initializer, generic stubs, version metadata, and py.typed are generated from the unified :api declaration in spork.it; none are maintained as parallel facade files. Runtime behavior is tested exclusively through native deftest declarations; mypy separately checks the generated Python typing facade.

spork sync --dev
spork test
mypy tests/typing/usage.py
spork dist --clean
python -m twine check dist/*

License

MIT

Metadata

Release files for spork-state 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spork-state 0.2.1
File Size Uploaded
spork_state-0.2.1.tar.gz 19.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spork-state 0.2.1
File Interpreter ABI Platform
spork_state-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 39.1 kB

Release files / spork_state-0.2.1.tar.gz

Download URL spork_state-0.2.1.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f323bb50634aa85d6938782fffd77ca03b6ce9c6dcea85397bd7fbba6215f5d7
BLAKE2b-256 checksum
How to use checksums
70780f1ba3fc7baf85574aa7acb7936c21379c33c187e3f4c380c2a6e3fc6bc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release files / spork_state-0.2.1-py3-none-any.whl

Download URL spork_state-0.2.1-py3-none-any.whl
Size 19.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b5f5e14c47dc42419a9941089b277f35d2e7ae7f3ea640385d984d481642f22
BLAKE2b-256 checksum
How to use checksums
7ac44e3aa91be9067409f32832748bd6ccf8508c646325bd628f17a19bacaad8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 30, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page