Skip to main content

deadliner

Monotonic deadline and time-budget primitive for bounding multi-step operations.

Create one Deadline, pass it down the call stack, and every step can ask how much time is left, stop with a typed error once it is gone, hand a safe timeout= to any blocking stdlib call, and carve out tighter budgets for sub-steps. Sync and async, fully typed, zero runtime dependencies.

Install

pip install deadliner

Usage

import queue
import subprocess

from deadliner import Deadline, DeadlineExceeded

deadline = Deadline(5.0)  # five seconds from now, on time.monotonic()

try:
    deadline.check("fetch")  # raises DeadlineExceeded once the budget is gone
    item = jobs.get(timeout=deadline.timeout())  # never negative, None if unbounded
    subprocess.run(["make", "build"], timeout=deadline.timeout(), check=True)
except DeadlineExceeded as exc:
    print(exc.step, exc.overrun)  # "fetch", seconds past the deadline

Derive tighter budgets. A child never outlives its parent, like Go's context.WithDeadline:

connect = deadline.child(1.0)  # at most 1s, and never past `deadline`

for step in deadline.split(3):  # three equal shares; unused time rolls over
    run_stage(timeout=step.timeout())

for step in deadline.split([1, 2, 1]):  # weighted shares
    ...

Wait without overshooting:

deadline.sleep(0.5)  # back off, but never past the deadline
deadline.wait_until(lambda: job.done, interval=0.1)  # poll; False on expiry
await deadline.sleep_async(0.5)
result = await deadline.wait_for(fetch(), "fetch")  # cancels fetch on expiry

An infinite deadline is a real value, so "no limit" needs no special cases:

deadline = Deadline.never()
deadline.timeout()  # None, which queue/subprocess/socket read as "block"
lock.acquire(timeout=deadline.timeout(unbounded=-1))  # -1 is Lock's "block"

The clock and sleeps are injectable, so tests never wait for real:

now = [0.0]
deadline = Deadline(10.0, clock=lambda: now[0], sleep=lambda s: None)
now[0] = 11.0
assert deadline.expired

See GUIDE.md for the full API, the rules it follows and recipes.

Command line

deadliner DURATION COMMAND... runs a command and stops it once the duration has passed, with the exit statuses of GNU timeout (124 on timeout). It works the same on Windows, where no such tool ships:

deadliner 30s -- python -m pytest
deadliner 5m make release

Why

Bounding a multi-step operation usually means scattering time.monotonic() arithmetic across the call stack, and a negative remainder passed straight to a stdlib call is a real bug: threading.Lock.acquire waits forever on -1 and raises ValueError on other negatives, and queue.Queue.get raises ValueError too. asyncio.timeout cancels a block of async code, but it is not an object you can pass to sync code, split across steps or check from a helper. deadliner is that object. The obvious package names do not fill this gap: timebudget is a speed profiler, and deadline wraps AWS Deadline Cloud.

License

MIT. See LICENSE.

Metadata

Release files for deadliner 0.2.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 deadliner 0.2.0
File Size Uploaded Provenance Actions
deadliner-0.2.0.tar.gz 24.2 kB Without provenance Details for deadliner-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 37.5 kB

Release files / deadliner-0.2.0.tar.gz

Download URL deadliner-0.2.0.tar.gz
Size 24.2 kB
Tags Source
SHA-256 checksum
How to use checksums
24b557a066e9eb712b64333d960f225adb1951aab60316af7df4ed5050478488
BLAKE2b-256 checksum
How to use checksums
b9ffa2252bb342ac16a176b349964a3091817ca8f5ce40994c3cff498ec810cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / deadliner-0.2.0-py3-none-any.whl

Download URL deadliner-0.2.0-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d15d9f1fccaaf3c02a37e1450ca41758e815884fef1f1e10dc4f125b25352b7
BLAKE2b-256 checksum
How to use checksums
6dc06df6b801b600006f63dc91df7b89a25c7f2ea959392995d70e1a2487d7d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for deadliner 0.2.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

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