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

Cache Invalidation

from runguard import guard, invalidate_cache

@guard(schedule="daily")
def generate_report(user_id):
    return {"user": user_id}

# remove one cached call signature
invalidate_cache(fn=generate_report, args=(1,))

# remove all cached entries
invalidate_cache()

Command Line Invalidation

# remove all entries from default state file
python -m runguard invalidate

# remove all entries from a custom state file
python -m runguard invalidate --state-path /var/tmp/myapp/runguard-state.json

# remove one specific cached call signature
python -m runguard invalidate \
    --module myapp.jobs \
    --function generate_report \
    --args '[1]' \
    --kwargs '{}' \
    --state-path /var/tmp/myapp/runguard-state.json

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
  • "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.3.0.tar.gz (4.8 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.3.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for runguard-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c396c1179805c685c524aad85e6fa96db855956d070a90633c487b6a1506caea
MD5 c5be257e3721ed4ebac5ae10eaed1630
BLAKE2b-256 a70e87fcfcaa346455d7cfa2c43313617bf0d8909ebffd49b4bae243df445c43

See more details on using hashes here.

File details

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

File metadata

  • Download URL: runguard-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8c337be07c3dd703ed029dec97a62e09af8506929085f835190acbec94a98d2
MD5 cb66e2506908dd0d81d8f1086e68064d
BLAKE2b-256 1afb95cff85a2dab9a1d6728d1b2087665640948e2a8eab8c2dec1b2e66df3bf

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