Minimal demand-driven query framework for incremental computation.
Project description
Cascade Query
Cascade Query is a Python library for incremental dependency tracking. It caches function results and re-executes them only when their specific inputs or upstream dependencies change.
Core Principles
- Automatic Caching: Results are stored. If dependencies are unchanged, the function body does not execute.
- Dependency Tracking: Cascade records every
@engine.inputor@engine.queryaccessed during execution. - Targeted Updates: When an input changes, Cascade identifies and invalidates only the affected downstream functions.
- Early Bail-out: If a function's output remains identical after its dependencies change, re-computation stops for that branch.
Quickstart
import time
from cascade import Engine
engine = Engine()
@engine.input
def user_id():
return "user_1"
@engine.query
def fetch_data():
time.sleep(2)
return {"id": user_id(), "data": "value"}
@engine.query
def get_result():
data = fetch_data()
return f"Result for {data['id']}"
# First run: Executes for 2 seconds.
print(get_result())
# Second run: Returns immediately from cache.
print(get_result())
# Update input:
user_id.set("user_2")
# Third run: Executes for 2 seconds to refresh.
print(get_result())
Engine API
Core Methods
Engine(max_entries=10000, stats=False): Initializes the engine.max_entriessets the limit for the Least Recently Used (LRU) cache.@engine.input: Decorator for mutable data roots.input.set(value): Updates the value and increments the global revision.input.set(*args, value=value): Updates a keyed input.
@engine.query: Decorator for cached computations.engine.snapshot(): Returns aSnapshotobject pinning the current global revision. Usequery(snapshot=s)to read data as it existed at that revision.engine.save(path)/engine.load(path): Persists all inputs and cached results to a SQLite database.
Parallel & Background Execution
engine.compute_many(calls, workers=None): Executes a list of queries in parallel using a thread pool.engine.submit(query, *args, executor=None): Schedules a query for background execution. Returns aconcurrent.futures.Future.QueryCancelled: Exception raised if a background query's dependencies change before it completes.
Graph Utilities
engine.inspect_graph(): Returns a dictionary of all nodes and edges in the dependency graph.engine.subgraph(roots, direction="deps"): Filters the graph to the dependency chain of the specified root nodes.engine.prune(roots): Removes cached query results that are not reachable from the specified roots.
Advanced Features
Side-Effect Accumulators
Queries must be pure functions. Use Accumulator to record side-effects (like logs or warnings) that must be replayed when a result is served from the cache.
warnings = engine.accumulator("warnings")
@engine.query
def validate_data():
data = fetch_data()
if not data:
warnings.push("No data found")
return data
# On cache hit, 'warnings' are re-populated into the effects dictionary.
effects = {}
validate_data(effects=effects)
print(effects["warnings"])
Performance Metrics
Set stats=True in the Engine constructor to track execution timing.
engine.stats_summary(): Returns wall-clock time spent in function bodies and cache eviction counts.engine.reset_stats(): Clears accumulated timing data.
Visualization
Cascade provides renderers for the dependency graph.
from cascade import export_dot, export_mermaid
graph = engine.inspect_graph()
# Generate Graphviz DOT format
dot_text = export_dot(graph)
# Generate Mermaid flowchart format
mermaid_text = export_mermaid(graph)
Limitations
- Cycle Detection: Cascade detects and rejects recursive function calls (cycles) with a
CycleError. - Thread Safety: While Cascade supports parallel query execution, the
Engineobject itself should be modified (.set(),@engine.query) from a single thread or with external synchronization. - Persistence Security:
engine.load()uses JSON to resolve types viaimportlib. Only load databases from trusted sources. - Python Version: Optimization for parallel CPU-bound work requires CPython 3.14+ free-threaded builds with
PYTHON_GIL=0.
Installation
pip install query-cascade
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 query_cascade-0.2.4.tar.gz.
File metadata
- Download URL: query_cascade-0.2.4.tar.gz
- Upload date:
- Size: 43.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b0b79ef1f9822f0b7a8dcb7eb9d8fe7180737eda0160835c6a1917bcf59d18a0
|
|
| MD5 |
ae523e9b620b49ce36f209f0e1e39bd4
|
|
| BLAKE2b-256 |
d6cfa2edb88fbf50f3d4dc5007ebcb3ad2aa29360fe9bf973d77e09c6a04a999
|
Provenance
The following attestation bundles were made for query_cascade-0.2.4.tar.gz:
Publisher:
workflow.yml on hmatt1/cascade-query
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
query_cascade-0.2.4.tar.gz -
Subject digest:
b0b79ef1f9822f0b7a8dcb7eb9d8fe7180737eda0160835c6a1917bcf59d18a0 - Sigstore transparency entry: 1392541615
- Sigstore integration time:
-
Permalink:
hmatt1/cascade-query@52bb5b40b249cccae507dfc884b33646062f1121 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/hmatt1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@52bb5b40b249cccae507dfc884b33646062f1121 -
Trigger Event:
push
-
Statement type:
File details
Details for the file query_cascade-0.2.4-py3-none-any.whl.
File metadata
- Download URL: query_cascade-0.2.4-py3-none-any.whl
- Upload date:
- Size: 22.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0d86084800675295c2738330fa9c3da14f496427a87de3c6cccc92d5b0407d6
|
|
| MD5 |
ee56e8eacea802777e7e87c82cacfda4
|
|
| BLAKE2b-256 |
89a4e717174cccd712594b6f5ca073e0a35cea781b59e753e12a960a99659219
|
Provenance
The following attestation bundles were made for query_cascade-0.2.4-py3-none-any.whl:
Publisher:
workflow.yml on hmatt1/cascade-query
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
query_cascade-0.2.4-py3-none-any.whl -
Subject digest:
a0d86084800675295c2738330fa9c3da14f496427a87de3c6cccc92d5b0407d6 - Sigstore transparency entry: 1392541616
- Sigstore integration time:
-
Permalink:
hmatt1/cascade-query@52bb5b40b249cccae507dfc884b33646062f1121 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/hmatt1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@52bb5b40b249cccae507dfc884b33646062f1121 -
Trigger Event:
push
-
Statement type: