Skip to main content

pacemaker

For controlling time per iteration loop in Python.

A carefully regulated loop comes in handy for managing rate-limited APIs, frame refreshes in animations, and cycles in real-time simulations. This package provides an accurate metronome for your code to keep time with, good up to 1 MHz (one microsecond per iteration).

The pacemaker package is intentionally minimalistic and has no external dependencies. It's a glorified snippet. The whole package's code is shorter than this README. Works on Linux, Windows, and MacOS.

Installation

pip install pacemaker-lite

Usage

from pacemaker.pacemaker import Pacemaker

iterations_per_second = 2.5
pm = Pacemaker(iterations_per_second)

for i in range(100):
    _ = pm.beat()
    print(i)

The repetitive import line can be better understood as

from pacemaker_package.pacemaker_module import PacemakerClass

What it does

The Pacemaker class keeps a loop operating at a steady rhythm, according to a wall clock. It's useful for coordinating the activities of several asynchronous and real-time processes.

Initialize Pacemaker(clock_frequency) with the desired number of iterations per second. Call Pacemaker.beat() in a loop to keep it cycling at the clock frequency.

If the pacemaker experiences a delay, it will allow faster iterations to try to catch up. Heads up: because of this, any individual iteration might end up being much shorter than suggested by the pacemaker's target rate.

beat() returns the amount of time that it was off from the ideal pace, counting back from the first beat.

off_by = pm.beat()

A return value of 0 means it was exactly correct. If it's higher than that, it means that the cycle came in late. It will usually be off by a little. You can create a set of checks on the return value if it's important to keep the cycle time tightly controlled.

a_little_late = .1  # seconds
a_lot_late = .5  # seconds
off_by = pm.beat()
if off_by > a_lot_late:
    raise RuntimeError(f"Last beat was running behind by {elapsed} seconds.")
if off_by > a_little_late:
    print(f"Last beat was running behind by {elapsed} seconds.")  # or log this

For a deep dive on what the pacemaker does and why, check out Chapter 2 of How to Train Your Robot: Keeping Time with Python.

Release files for pacemaker-lite 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pacemaker-lite 0.1.3
File Size Uploaded
pacemaker_lite-0.1.3.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pacemaker-lite 0.1.3
File Interpreter ABI Platform
pacemaker_lite-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 7.8 kB

Release files / pacemaker_lite-0.1.3.tar.gz

Download URL pacemaker_lite-0.1.3.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3ce4629ae179fa6bc19958d50fce9c69ff5a3b7263ae60838e81efd6fa37cb1b
BLAKE2b-256 checksum
How to use checksums
cf2dc1f4604b46104a5b32c088b15dfce0114a0fb3be2c20f93debf85366ce11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release files / pacemaker_lite-0.1.3-py3-none-any.whl

Download URL pacemaker_lite-0.1.3-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f5c8a50d26d0846c9777964a378e1fe22674f85b9443083050c4e7886d51d237
BLAKE2b-256 checksum
How to use checksums
08de362c21717089e29ad139f31a386c4a5d3175b2ea34c21bf7974e46edd48f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page