Skip to main content

naptime is a Python library to provide sub-millisecond sleep accuracy.

Project description

naptime

Sub-millisecond sleep accuracy for Python.

Why naptime?

Python's built-in time.sleep() guarantees a minimum sleep duration, but frequently overshoots the target — sometimes by several milliseconds. For applications that need precise timing, that difference matters.

naptime solves this with a hybrid strategy:

  1. OS sleep handles the bulk of the wait, keeping CPU usage low.
  2. Busy-wait (time.perf_counter()) takes over for the final microseconds, delivering the accuracy that time.sleep() cannot.

The crossover point is configurable, so you can tune the trade-off between accuracy and CPU cost. naptime has zero dependencies and supports Python 3.10+.

Installation

pip install naptime

or

uv add naptime

Usage

Drop-in replacement for time.sleep()

import naptime

# Sleep for 500 ms — works just like time.sleep(),
# but with much better accuracy.
naptime.sleep(0.5)

Get the actual elapsed time

naptime.sleep() returns the actual time slept (in seconds):

actual = naptime.sleep(0.1)
print(f"Requested 100 ms, actually slept {actual * 1000:.2f} ms")

Tune accuracy vs. CPU usage

The wait_threshold_s parameter controls when naptime switches from OS sleep to busy-waiting. A larger threshold means more time spent busy-waiting (higher accuracy, higher CPU usage):

# Busy-wait the last 20 ms instead of the default 10 ms
naptime.sleep(0.1, wait_threshold_s=0.02)

Pure busy-wait

For the tightest timing requirements you can bypass OS sleep entirely:

# Busy-wait for 50 ms
naptime.busy_wait(0.05)

Full API reference

naptime.MIN_SLEEP_TIME  # Platform default: 1 ms (Linux/macOS), 4 ms (Windows)

naptime.sleep(
    seconds,                        # Duration to sleep (s)
    wait_threshold_s=0.01,          # Switch to busy-wait below this (s)
    min_sleep_time=MIN_SLEEP_TIME,  # Floor for OS sleep calls (s)
    busy_wait_interval=None,        # Optional sleep inside the busy loop (s)
) -> float                          # Actual elapsed time (s)

naptime.busy_wait(
    seconds,          # Duration to busy-wait (s)
    interval=None,    # Optional sleep inside the loop (s)
) -> float            # Actual elapsed time (s)

License

naptime is licensed under the Apache License 2.0.

Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES.

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

naptime-1.0.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

naptime-1.0.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: naptime-1.0.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for naptime-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f74958a051ee2314d23b5ae225e8ebd34d3de00c78b192f15417a934a938180d
MD5 ecde071ca7e3b5cdee017bc613c40494
BLAKE2b-256 93f3a1419e1b602bed7c9535df0ae7a13cb4e372ca70527f06ecbe9a5eee620c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: naptime-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for naptime-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2e1726fd06663a09cd6dbb902563da66733e732dcf701d1c8b105062be55ebd7
MD5 dd6a04532cd23b64950a5dec3db86d30
BLAKE2b-256 a9ce1f2ca5f6f721d6a03de29ec1842e2ba2d49abf74640cfc393161ec5afbc9

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