Skip to main content

Sleep until a specific time

Project description

sleep_until

This module provides a function sleep_until(deadline_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. Limited testing has shown that sleep_until should still work in a predictable fashion (either returning immediately if the system clock is adjusted back, or sleeping until the specified wall-clock time if the system clock is adjusted forwards), however, this behavior can currently not be guaranteed by this module, so be sure to test the behavior on your system if you expect to be making adjustments to the system clock while sleep_until is running (including via a background NTP service).

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

Here is how one might implement a loop that executes at a specific 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 wake-up 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.1.tar.gz (11.8 kB view details)

Uploaded Source

Built Distributions

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

sleep_until-1.1-cp314-cp314-win_amd64.whl (9.7 kB view details)

Uploaded CPython 3.14Windows x86-64

sleep_until-1.1-cp314-cp314-win32.whl (9.5 kB view details)

Uploaded CPython 3.14Windows x86

sleep_until-1.1-cp313-cp313-win_amd64.whl (9.6 kB view details)

Uploaded CPython 3.13Windows x86-64

sleep_until-1.1-cp313-cp313-win32.whl (9.3 kB view details)

Uploaded CPython 3.13Windows x86

sleep_until-1.1-cp312-cp312-win_amd64.whl (9.6 kB view details)

Uploaded CPython 3.12Windows x86-64

sleep_until-1.1-cp312-cp312-win32.whl (9.3 kB view details)

Uploaded CPython 3.12Windows x86

sleep_until-1.1-cp311-cp311-win_amd64.whl (9.6 kB view details)

Uploaded CPython 3.11Windows x86-64

sleep_until-1.1-cp311-cp311-win32.whl (9.3 kB view details)

Uploaded CPython 3.11Windows x86

sleep_until-1.1-cp310-cp310-win_amd64.whl (9.6 kB view details)

Uploaded CPython 3.10Windows x86-64

sleep_until-1.1-cp310-cp310-win32.whl (9.3 kB view details)

Uploaded CPython 3.10Windows x86

File details

Details for the file sleep_until-1.1.tar.gz.

File metadata

  • Download URL: sleep_until-1.1.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sleep_until-1.1.tar.gz
Algorithm Hash digest
SHA256 aeb79fe10e4ecd0adcd8f97d9d2059710b16478e3668d0979d7dd29f8e4e4145
MD5 b7a3a5798146244246d39e8adb529249
BLAKE2b-256 75a2e8283749a630fec64128bf5ccd0b108fcaca2c0aba3f10362803d6e265b6

See more details on using hashes here.

File details

Details for the file sleep_until-1.1-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: sleep_until-1.1-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sleep_until-1.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 96f6d5300b7a2b9cc1ae72631a3313d3b8af7c6dda3d81fa810266353f10e6bf
MD5 6be84dc0d9eeafc18076ac5412cdb939
BLAKE2b-256 d76cda16abf4e35b1f6e52965bb9e4c4fa3ac176d5d4ce5003067b4e20393e08

See more details on using hashes here.

File details

Details for the file sleep_until-1.1-cp314-cp314-win32.whl.

File metadata

  • Download URL: sleep_until-1.1-cp314-cp314-win32.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: CPython 3.14, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sleep_until-1.1-cp314-cp314-win32.whl
Algorithm Hash digest
SHA256 a58159b41a73e6ab1e35824c8eafb4db32f44a907351b491580c4239d478fa45
MD5 525bc6c6b4046ca63f893372e30fc74a
BLAKE2b-256 f31669d946e2c6caa77b19a437ae2443b344b724eb8beed0ebf57df76b370ac1

See more details on using hashes here.

File details

Details for the file sleep_until-1.1-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: sleep_until-1.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sleep_until-1.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a89bbf8ac29b58ff3960eb013f9e700646e8d2404754894fe9c905dc9e0fafa1
MD5 164702da9a3c68d18b8b4c9d353c031b
BLAKE2b-256 48fd600e694674fb288ba93d3d059aed69b2f826678be66646284a2c01b8bf5e

See more details on using hashes here.

File details

Details for the file sleep_until-1.1-cp313-cp313-win32.whl.

File metadata

  • Download URL: sleep_until-1.1-cp313-cp313-win32.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: CPython 3.13, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sleep_until-1.1-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 2ee68a408cf7982fa3f77eb41059484783ba99956a50ac788ba181a348b84bf3
MD5 6fc1b8a4a45385547e2606f36f18a7bb
BLAKE2b-256 65c745f478f4400bc173446a6de4e0552cb0c44d6354068c557f17bde9b8495a

See more details on using hashes here.

File details

Details for the file sleep_until-1.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: sleep_until-1.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sleep_until-1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7b0dc58e72831c949b944c4f884306b03b3bc5fabfde1f1b13f7da3373b009f4
MD5 b933e2446480927be15c8643395f85f6
BLAKE2b-256 3f0ac2e83e9a3c15cbeb8ef9502c223b22f60cbbeb662a2b30c099158cff9fee

See more details on using hashes here.

File details

Details for the file sleep_until-1.1-cp312-cp312-win32.whl.

File metadata

  • Download URL: sleep_until-1.1-cp312-cp312-win32.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: CPython 3.12, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sleep_until-1.1-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 263b7dca1821300633fb2d840250bede94365c637e581beb2eb8fadb1a1b0271
MD5 987f3b1327e592a9f91a4ad40d721a72
BLAKE2b-256 8207bcac5d1900ebc90c1a78b11069e012f71ff35ea0a483a743f08045b03c44

See more details on using hashes here.

File details

Details for the file sleep_until-1.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: sleep_until-1.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sleep_until-1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1d2cc9d5cd2ae907a532c36bd2e06eb41e4f6479d5ae8b1c8dd2baaf0d83b73a
MD5 12ae4c869547df0901afb33287db411b
BLAKE2b-256 3bc6db0f7e78a87cc70744bc71a4913c24930d7b10bd8b0e4fb160f28fe856a0

See more details on using hashes here.

File details

Details for the file sleep_until-1.1-cp311-cp311-win32.whl.

File metadata

  • Download URL: sleep_until-1.1-cp311-cp311-win32.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: CPython 3.11, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sleep_until-1.1-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 7a63c39538892c1c5ba3a14625214ecc996c0d3691ebe1f17cb6b1f0706e122f
MD5 3cf1cbe8ee305f45624ed2338731499e
BLAKE2b-256 91191a9fa8f8b6eac46bf828ab57c13574f4d129003b4b605f2087531371526e

See more details on using hashes here.

File details

Details for the file sleep_until-1.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: sleep_until-1.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sleep_until-1.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 823e74cd1ec7bfc64a6fda49ff48a423fab07f97f03cc38a07061a9f108fcbf9
MD5 cd90b8d570df75d3d3ad4d06726d64fa
BLAKE2b-256 e83bc820ce83243eabfe93a20923424623096890780d2b5f165e3fa788b3c635

See more details on using hashes here.

File details

Details for the file sleep_until-1.1-cp310-cp310-win32.whl.

File metadata

  • Download URL: sleep_until-1.1-cp310-cp310-win32.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: CPython 3.10, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sleep_until-1.1-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 492361800581071b8e459ef4c113842f1576ee37f0bc72fca7367cc1de226482
MD5 a024fd8da254acd83b1780129bd24d62
BLAKE2b-256 3eb98939706bb824ad223a75d49450b7a312ac0b661d5db9b0b48c2613832b62

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