Skip to main content

Context managers that enforce a minimum cool-down interval between consecutive blocks (sync and asyncio).

Project description

cooltime-guard

A micro library to guard execution with a cooldown interval. It ensures that a block of code runs only after a specified minimum time has passed since the previous execution. If not enough time has passed, it waits (time.sleep) automatically before running.

Features

  • Simple and lightweight
  • Context manager support with with syntax
  • Guarantees a minimum interval between executions
  • Useful for hardware control, API calls, or any resource that needs cooldown

Examples

A simple example:

import time
import cooltime_guard

# create a guard with 5ms interval
guard = cooltime_guard.Guard(interval=0.005)

def f():
    start = time.time()
    with guard:
        # ... Access to resources requiring a 5ms cooldown ...
        return time.time() - start

print(f()) #=> Nearly 0
print(f()) #=> Nearly 0.005
print(f()) #=> Nearly 0.005
time.sleep(0.01)
print(f()) #=> Nearly 0

When controlling LEDs that require a minimum interval of 1ms to light up properly:

import cooltime_guard

# create a guard with 5ms interval
cg = cooltime_guard.Guard(interval=0.005)

def led_flash():
    with cg:
        GPIO.Pin(15).flash()

Use cases

  • Hardware access that requires cooldown (e.g., LEDs, sensors, actuators)
  • API clients that must avoid calling too frequently
  • Rate-limiting function calls without complex frameworks

Notes

  • Guard does not perform lock control for threading/multiprocessing. Therefore, if code blocks execute in parallel, the intended behavior may not occur. In such cases, use locks in conjunction with guards.

License

MIT License

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

cooltime_guard-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

cooltime_guard-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cooltime_guard-0.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cooltime_guard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bd1744dae0cd8440db89f8afd1fb4e2ce61131aeec4d68c484d42a579f58bfd7
MD5 5b1b7500ec2fd9cd1c9785387a720cdf
BLAKE2b-256 2848bef358fe476d492f6c2373499f53af7672e6d0d8c52f885d39a9c4784691

See more details on using hashes here.

Provenance

The following attestation bundles were made for cooltime_guard-0.1.0.tar.gz:

Publisher: python-publish.yml on nakat-t/cooltime-guard-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: cooltime_guard-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cooltime_guard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bfbb2949b395a601418036bea76f69c102710d9fe94d04c4f36b6ba0f9b1be63
MD5 cee5e70e20b58dc1585c678e8ab8c09b
BLAKE2b-256 7ca84a2b0fdd1d28adb541cc5a181d5cb29f068bdfc9a1ed1a1baad8176f7472

See more details on using hashes here.

Provenance

The following attestation bundles were made for cooltime_guard-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on nakat-t/cooltime-guard-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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