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-1.0.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-1.0.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: runguard-1.0.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-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6bb7126f351ee282e574b460bf43e3b31d2b397b298bc5088311609cf2af7557
MD5 8553967ed71609bf82225fa7c7475ef9
BLAKE2b-256 4b5935d0a1571899374db615c732c9a9a95f521728d1b5f0104e76821a1f39d9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: runguard-1.0.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-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 246b7e092307bf925d4ee9851ffa9b5e77065d271a8fd5dae3833b70e5015438
MD5 e9c679563d32a25e4176b589092e16f7
BLAKE2b-256 23467fb8247555ab28d84fe6c070eff972448181657a229c83206e6abdb3dfe4

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