Registry / observability / monotonic

monotonic

JSON →
library1.6pypypi✓ verified 25d ago

This library provides a backport of Python's `time.monotonic()` function for older Python environments (Python 2 and Python < 3.3). It offers a monotonic clock, guaranteed not to go backward, which is crucial for reliably measuring elapsed time, as it remains unaffected by system clock adjustments such as daylight saving changes. The current version is 1.6. The library is considered stable and complete, with no further updates planned, as `time.monotonic()` is integrated into the standard library for Python 3.3 and newer.

pip install monotonic
INSTALL
IMPORT
SIG · MONOTONIC
M
monotonic
observabilitypythonv1.6
Install
1.5s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

monotonic
import monotonic start = monotonic.monotonic()
import time start = time.monotonic()
The `monotonic` package provides the `monotonic()` function. For Python versions < 3.3, `time.monotonic()` does not exist in the standard library. For cross-version compatibility, you might use a conditional import: `try: from time import monotonic as get_monotonic_time_fn except ImportError: from monotonic import monotonic as get_monotonic_time_fn`

This quickstart demonstrates how to use the `monotonic` function to measure elapsed time, ensuring that clock adjustments do not affect the duration measurement. It includes a robust import pattern for compatibility across Python 2 and Python 3.3+.

import time # For compatibility across Python versions, prefer this pattern: try: from time import monotonic as get_monotonic_time_fn except ImportError: # Fallback for Python 2 and Python < 3.3 using the 'monotonic' package from monotonic import monotonic as get_monotonic_time_fn print('Starting a monotonic timer...') start_time = get_monotonic_time_fn() # Simulate some work time.sleep(2.5) end_time = get_monotonic_time_fn() elapsed_time = end_time - start_time print(f'Elapsed time: {elapsed_time:.3f} seconds')
Debug
Known issues
deprecatedFor Python 3.3 and newer, `time.monotonic()` is part of the standard library. Using this `monotonic` package on these versions is redundant and unnecessary, though it will gracefully alias the built-in function. Prefer `time.monotonic()` directly for modern Python versions to avoid unnecessary dependencies.
fix
Remove `monotonic` from your dependencies and use `from time import monotonic` directly.
affects: Python 3.3+
gotchaThe value returned by `monotonic.monotonic()` (or `time.monotonic()`) has an undefined reference point. Only the *difference* between two calls to the function is meaningful for measuring elapsed time; the absolute value itself should not be relied upon. Comparing values from different processes might also be unreliable on some OS.
fix
Always calculate durations by subtracting an earlier `monotonic()` call from a later one (e.g., `end_time - start_time`).
affects: All versions
gotchaThe precision of the monotonic clock can vary by operating system. Specifically, on some Windows implementations, `monotonic.monotonic()` (and `time.monotonic()`) might offer less decimal precision compared to `time.time()`, typically around three decimal places.
fix
Be aware of potential precision limitations on Windows. If nanosecond precision is required on modern Python, `time.monotonic_ns()` might be an alternative (available in Python 3.7+), but this library does not provide that.
affects: All versions on Windows
breakingIf the `monotonic` library cannot find a suitable underlying system implementation for a monotonic clock on the current platform, attempting to import the module or its functions will raise a `RuntimeError`.
fix
Ensure the target platform has a compatible system clock. Common platforms like Linux, Windows, macOS, BSD, and AIX are supported.
affects: All versions on unsupported platforms
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'monotonic'
The 'monotonic' library has not been installed in your Python environment.
fix
Install the library using pip: `pip install monotonic`
ImportError: cannot import name monotonic
This error typically occurs when trying to import `monotonic` directly from the `time` module in Python versions older than 3.3, or if the external `monotonic` library is installed but imported incorrectly.
fix
For Python versions where `time.monotonic()` is not available, import it from the installed `monotonic` library: `from monotonic import monotonic` or `from monotonic import monotonic as time_monotonic`
AttributeError: module 'time' has no attribute 'monotonic'
You are attempting to use `time.monotonic()` in a Python environment older than 3.3, where this function is not part of the standard `time` module.
fix
Install the `monotonic` backport library (`pip install monotonic`) and then import and use its function: `from monotonic import monotonic` (or alias it, e.g., `from monotonic import monotonic as timer_monotonic`)
Upgrade
Version history
1.6latest on PyPI · released Apr 9, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
26 hits · last 30 days
node
20
OpenAI (training)
2
Resources
monotonic — pip install monotonic · libregistry