Skip to main content

interval-timer

interval-timer is a Python package that enables iterating over a sequence of regular time intervals with high precision.

Installation

Install from PyPI via:

pip install interval-timer

Usage

Basic usage is as follows:

from interval_timer import IntervalTimer

for interval in IntervalTimer(0.5):
    print(interval)
    
    # Execute code exactly every half second here
    ...

Output:

Interval(index=0, time=0.000, lag=0.000)
Interval(index=1, time=0.500, lag=0.000)
Interval(index=2, time=1.000, lag=0.000)
...

For more usage examples see examples/.

Description

IntervalTimer is an iterator object that returns Interval objects at regular time intervals. Code can then be executed upon each time interval, and the intervals will stay synchronised even when the code execution time is non-zero.

IntervalTimer is a more precise replacement for a loop that contains a wait. The following code:

from time import sleep

# Iterates approximately every half second
for i in range(5):
    print(i)
    sleep(0.5)

can be replaced with:

from interval_timer import IntervalTimer

# Iterates exactly every half second
for interval in IntervalTimer(0.5, stop=5):
    print(interval)

interval-timer uses perf_counter under the hood to obtain high precision timing. It will not suffer from drift over long time periods.

If an interval iteration is delayed due to slow code execution, then future intervals will still be synchronised to absolute time if they're given time to catch up. The caller can see if synchronisation has been temporarily lost by checking if the Interval object's lag attribute returns a non-zero value (see the lag.py example).

Timing diagram

Timing diagram

The above timing diagram shows that each returned Interval object has the following attributes:

  • time: the nominal start time of the interval. Always has equal value to the end_time value of the previous interval.
  • buffer: the length of time before the interval start time that the interval was requested. The minimum buffer is zero.
  • lag: The length of time after the interval start time that the interval was requested. The minimum lag is zero. If the lag is non-zero, then the code executed within the previous interval took longer than the interval period, which is generally undesirable.

Metadata

Release files for interval-timer 1.0.0

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

Source distribution (sdist)

Source distribution for interval-timer 1.0.0
File Size Uploaded
interval_timer-1.0.0.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for interval-timer 1.0.0
File Interpreter ABI Platform
interval_timer-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.5 kB

Release files / interval_timer-1.0.0.tar.gz

Download URL interval_timer-1.0.0.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c527434ed846e8d69ab59850af6d876d69b1d390b73ec32cb2fda4fd94b22015
BLAKE2b-256 checksum
How to use checksums
db08a0679c4643fcff8970a0cf98bc6d211a10784efa25980f472dbbbcee526d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.10.8 Darwin/21.6.0

Release files / interval_timer-1.0.0-py3-none-any.whl

Download URL interval_timer-1.0.0-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e49a43f419f622644761256676f5286a44d3dd2d8cf826ae1996fb8d56d3b189
BLAKE2b-256 checksum
How to use checksums
a75f26310dbe43d8bcbdf28c21317099bd61ab5a6dcf5a433cbfc66209ef3eb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.10.8 Darwin/21.6.0

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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