Skip to main content

globalstash

Thread-safe, process-wide global state you can import from any module at any depth, with no sys.path tricks.

pip install globalstash

Usage

from globalstash import g

# anywhere in your app
g.db_pool = create_pool()
g["config"] = {"debug": True}

g.get("missing", "default")
g.setdefault("cache", {})
g.update(env="prod", version="1.0")
"db_pool" in g
del g.db_pool

Because Python caches modules, every from globalstash import g returns the same object.

Atomic and compound operations

g.incr("requests")          # atomic counter

with g.lock:                # read-modify-write
    g.total = g.total + 1

Named namespaces

from globalstash import get_state

cache = get_state("cache")   # same name -> same object everywhere
cache.ttl = 60

Tests

with g.temporary(debug=True):
    ...   # values restored on exit

Notes

  • Method names (get, pop, update, ...) can't be used as attribute keys; use g["get"].
  • State lives per process. Gunicorn/Uvicorn workers each have their own copy. Use Redis or a database for state shared across processes.
  • For per-request state (like flask.g), use contextvars instead.

License

MIT

Metadata

Release files for globalstash 0.1.0

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

Source distribution (sdist)

Source distribution for globalstash 0.1.0
File Size Uploaded
globalstash-0.1.0.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for globalstash 0.1.0
File Interpreter ABI Platform
globalstash-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / globalstash-0.1.0.tar.gz

Download URL globalstash-0.1.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f21333ba2306f20022177880fc787d922739c95f45132fb8c74cd45babe30d7c
BLAKE2b-256 checksum
How to use checksums
7083174dda673bfacfa256b90409744b47ec6543bef3265a446173dd73cc4638
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release files / globalstash-0.1.0-py3-none-any.whl

Download URL globalstash-0.1.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1f1320c5b2aecc574c9472db2a0f9407c01a67d3a5b18a8a2b0305f2009a503a
BLAKE2b-256 checksum
How to use checksums
476612a668ebfe9669f9d06be9524123b5adb67ef1601cfdbd6d2b84b5007cbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.0 This release

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