Mid-execution distributed locking with zero boilerplate.
Project description
caesura 📜
Mid-execution distributed locking with zero boilerplate.
Standard distributed locks force you to wrap your entire function in a with block, even if you need to do heavy data parsing or database lookups just to find out what needs locking.
caesura solves this by letting you safely acquire locks mid-execution. By using the native yield keyword, you signal exactly where the blocking I/O happens, and the decorator safely manages the teardown—even if the function crashes.
uv add caesura
The Difference
| Concept | Standard Context Managers | caesura |
|---|---|---|
| Lock Boundary | The entire function. | Exactly where it matters. |
| Deadlock Prevention | Manual ordering required. | Mathematically guaranteed via Tuples. |
| Infrastructure Coupling | High (Imports Redis/Postgres). | Zero (Accepts any Context Manager). |
Usage
Simply use the @caesura.serialize decorator and yield your standard context managers when you are ready to lock.
import caesura
@caesura.serialize
def process_webhook(payload):
# 1. Do heavy, lock-free work first
parsed_data = heavy_json_parse(payload)
# 2. Yield hands the lock to the decorator.
# This visually signals the blocking I/O to the reader.
yield redis.lock(f"user:{parsed_data.user_id}")
# 3. Execution resumes safely locked.
db.update_balance(parsed_data.user_id)
# When the function returns (or if it crashes!),
# caesura safely releases all acquired locks via Python's native ExitStack.
return True
Deadlock Prevention
The Dining Philosophers deadlock occurs when Worker A locks alice then bob, and Worker B locks bob then alice.
caesura solves this statelessly. If your transaction requires multiple resources, yield them as a Python tuple. caesura will intercept the tuple, lexicographically sort the locks by their identifier, and acquire them in an identical, deterministic order across all workers.
@caesura.serialize
def transfer_funds(sender_id, receiver_id):
# Even if receiver_id evaluates first alphabetically,
# caesura sorts them before acquiring. Deadlocks are mathematically impossible.
yield (
redis.lock(sender_id),
redis.lock(receiver_id)
)
db.execute_transfer(sender_id, receiver_id)
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 caesura-0.1.0.tar.gz.
File metadata
- Download URL: caesura-0.1.0.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22d554eef0b64971681f0652d5d46163596abf0d54b6131f2249a0bdb89a3bf0
|
|
| MD5 |
1dc21fcc6376a9286e572004d764b1bb
|
|
| BLAKE2b-256 |
d0073ef1008d25468dd27aa4c019b33954b6630ce56bbce361ec9e846aefb442
|
File details
Details for the file caesura-0.1.0-py3-none-any.whl.
File metadata
- Download URL: caesura-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CachyOS Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
889553c4b3c5c3d9ffe7d4be4a1200f1a39254346bdd98b0f79b98defdc79796
|
|
| MD5 |
8629cc8739f57baf4dfc2ffada9e8432
|
|
| BLAKE2b-256 |
5d425ed5d588c2953b21c6762b0bb3610d2aa1d3cc43f779e993c9fb4a1e18d2
|