Skip to main content

Make Python functions safe to run more than once

Project description

runguard

Make Python functions safe to run more than once.

runguard ensures a function only executes once within a given time window (e.g. once per day, per hour, or per input) — even across multiple processes.


Why?

In real systems, the same function can get called multiple times:

  • retries
  • cron jobs
  • API triggers
  • AI-generated scripts
  • accidental double execution

This can lead to:

  • duplicate DB writes
  • repeated API calls
  • inconsistent state

runguard prevents that with a simple decorator.


Install

pip install runguard  # (or drop the file into your project)

Quick Example

from runguard import guard

@guard(schedule="daily")
def generate_report(user_id):
    print("Running expensive job...")
    return {"user": user_id}

generate_report(1)
generate_report(1)  # skipped
generate_report(2)  # runs (different input)

Features

  • Runs once per time window (daily, hourly, weekly, or custom TTL)
  • Parameter-aware (per input)
  • Cross-process safe (file locking, works on Windows & Unix)
  • Returns cached result
  • Zero infrastructure required

Schedules

Daily / Hourly / Weekly

@guard(schedule="daily")
@guard(schedule="hourly")
@guard(schedule="weekly")

Custom TTL

@guard(ttl_seconds=600)  # once every 10 minutes

Intended Usage

  • scripts that may be triggered multiple times
  • cron jobs / schedulers
  • API endpoints with side effects
  • anything that “should not run twice”

Future Improvements

  • SQLite / Redis backend
  • async support
  • manual invalidation
  • "run but don’t cache result" mode

Notes

runguard is not a framework.

It’s a small, reliable utility to make unsafe execution safe by default.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

runguard-0.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

runguard-0.1.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file runguard-0.1.0.tar.gz.

File metadata

  • Download URL: runguard-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for runguard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e478d112dbb02c9ee13027f27e5fa7309e310e1f3000e61040507bfb5e546d0f
MD5 713c26bc816731cf74008100fc34f977
BLAKE2b-256 76ca6eec5251765e11c812779643960b0c0b4aed29ab72c11e27da32e5a0c26e

See more details on using hashes here.

File details

Details for the file runguard-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: runguard-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for runguard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eaaf6061209f321d836ec8d2a553adb31a84c25e79630f452ac70232b3f82ede
MD5 5533feaf80d7e63b424c24f9449c83c8
BLAKE2b-256 d23ce0c4b3124e63dbeb335b11233b5a282a80d8313ca18ecf633350e3be8302

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page