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.3.tar.gz (3.4 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.3-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: runguard-0.1.3.tar.gz
  • Upload date:
  • Size: 3.4 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.3.tar.gz
Algorithm Hash digest
SHA256 317b8ebe745e9ee94e96622471fad0e8af7f8b3493871e34511137d0ae2c7d3c
MD5 8966a5ae4c46c2946de2d8483d98b50b
BLAKE2b-256 8e5caee1e2eeacffe70e6722e744143066daa8502ea38c54c6c15fa14d848db5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: runguard-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 36d4dc5182269cb9c970b97cf0bfdeedf6ada1d1e16943283ff241a94f8232c0
MD5 11129c6213c717eef0a0b3b4653a82e4
BLAKE2b-256 a7c2de62638076dd3a198ab845c1f968f51f36a25d572957ff2bcf1c6887c0f1

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