Skip to main content

Sleep until a specific time

Project description

sleep_until

This module provides a function sleep_until(seconds), which is like time.sleep(), but it sleeps until the specified time of the system clock as returned by time.time(). This can be used, for example, to schedule events at a specific timestamp obtained from datetime.datetime.timestamp().

See the notes in time.sleep() on the behavior when interrupted and on accuracy. Additionally, because this function uses the system clock as a reference (CLOCK_REALTIME on Unix), this means the reference clock is adjustable and may jump backwards.

  • On Windows, SetWaitableTimerEx is used, in combination with CREATE_WAITABLE_TIMER_HIGH_RESOLUTION if available. Precompiled "wheels" for CPython are available on PyPI.
  • On POSIX systems, clock_nanosleep(2) is used, so this must be available, along with the appropriate tools to compile the module.
  • On Mac OS X, at the time of writing, clock_nanosleep is not available, so the module currently does not build there.

The test.py script can be used to test the functionality of the module.

Here is how one might implement a loop that executes at a fixed interval:

from time import time
from sleep_until import sleep_until

interval_s = 1

# using "int" here to start on a full second
next_s = int(time()) + interval_s
while True:
    # calculate the next wakeup time and sleep until then
    now_s = time()
    # if the user's code takes longer than the interval, skip intervals
    while next_s < now_s: next_s += interval_s
    sleep_until(next_s)

    # run any user-specified code here
    print(time())

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

sleep_until-1.0.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distributions

sleep_until-1.0-cp312-cp312-win_amd64.whl (8.7 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

sleep_until-1.0-cp312-cp312-win32.whl (8.4 kB view hashes)

Uploaded CPython 3.12 Windows x86

sleep_until-1.0-cp311-cp311-win_amd64.whl (8.6 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

sleep_until-1.0-cp311-cp311-win32.whl (8.3 kB view hashes)

Uploaded CPython 3.11 Windows x86

sleep_until-1.0-cp310-cp310-win_amd64.whl (8.6 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

sleep_until-1.0-cp310-cp310-win32.whl (8.3 kB view hashes)

Uploaded CPython 3.10 Windows x86

sleep_until-1.0-cp39-cp39-win_amd64.whl (8.6 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

sleep_until-1.0-cp39-cp39-win32.whl (8.3 kB view hashes)

Uploaded CPython 3.9 Windows x86

sleep_until-1.0-cp38-cp38-win_amd64.whl (8.6 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

sleep_until-1.0-cp38-cp38-win32.whl (8.3 kB view hashes)

Uploaded CPython 3.8 Windows x86

sleep_until-1.0-cp37-cp37m-win_amd64.whl (8.6 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

sleep_until-1.0-cp37-cp37m-win32.whl (8.3 kB view hashes)

Uploaded CPython 3.7m Windows x86

sleep_until-1.0-cp36-cp36m-win_amd64.whl (7.5 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

sleep_until-1.0-cp36-cp36m-win32.whl (7.4 kB view hashes)

Uploaded CPython 3.6m Windows x86

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page