-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow sleeping until an absolute time #101558
Comments
Adds the `time.sleep_until` function, which allows sleeping until the specified absolute time.
Worth pointing out that I would love to have the equivalent functions like |
I checked the implementation, and |
Adds the `time.sleep_until` function, which allows sleeping until the specified absolute time.
Additional arguments for this feature suggestion provided here: #101559 (comment) |
Adds the `time.sleep_until` function, which allows sleeping until the specified absolute time.
I think that for |
I still don't know of an async equivalent of the system call |
Adds the `time.sleep_until` function, which allows sleeping until the specified absolute time.
Adds the `time.sleep_until` function, which allows sleeping until the specified absolute time.
I'd love to see the PR attached here merged. To implement a periodic task in a Python loop, one is currently forced to do a racy subtraction with the current time. The pull request attached to this proposal fixes that. To print 'A' at 100hz, one might naively write:
...but of course, that's not really going to yield a coherent 100hz. There are many applications where that matters, such as sampling a sensor to produce timeseries data. If you actually care, you're forced to do something like this:
...but that's inherently racy. If this proposal is implemented, that loop could become:
...which both simplifies the code, and eliminates the race condition. On platforms where there isn't support for TIMER_ABSTIME, it could fall back to the racy subtraction (which is what the user would've done anyway). |
pganssle's comment sums up the current state here: #101559 (comment) |
I propose a version of
time.sleep()
that allows for the specification of absolute times.Python 3.11 added the use of
clock_nanosleep
intime.sleep()
(#28111), and both it and Windows'SetWaitableTimerEx
allow for the specification of absolute times. This can be useful for writing simple loops that trigger at an interval that is tied to wall-clock time, which has many applications: triggering taking of measurements, pictures, checking status, sending messages, ...With
time.sleep()
, one has to emulate this by sayingtime.sleep(deadline - time.time())
, whichpysleep
then calculates back into an absolute time internally, which adds inaccuracy.I will be submitting a pull request shortly that modifies
pysleep
to give it an "absolute
" argument and adds atime.sleep_until()
function.Linked PRs
The text was updated successfully, but these errors were encountered: