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.1.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.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: runguard-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 23989db83f6f3aaac7142fb7c19fff6f04ac55dce0af8045b831d729f38e6c1b
MD5 8e7b233a1c2c6cb61a59680dc9697440
BLAKE2b-256 ac91e83bc5abe71342e9b80974a926c1d78970b0afa52bbf4b81d90712351fe9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: runguard-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2aee43e2e8fce61f270bc14c8e79ec5e9a52d807647fe0820e6f020c9c9273e8
MD5 4eb7a4af91901ae2710ab9e196109315
BLAKE2b-256 ba134cc58e06269c76d0b02571a3851e5fd5a27429a1620f64ed56def421d323

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