Mailing List Archive

[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux
Change by Roundup Robot <devnull@psf.upfronthosting.co.za>:


----------
keywords: +patch
nosy: +python-dev
nosy_count: 4.0 -> 5.0
pull_requests: +26521
stage: -> patch review
pull_request: https://github.com/python/cpython/pull/28077

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Change by Benjamin Sz?ke <egyszeregy@freemail.hu>:


----------
nosy: +Livius
nosy_count: 5.0 -> 6.0
pull_requests: +26552
pull_request: https://github.com/python/cpython/pull/28111

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Benjamin Sz?ke <egyszeregy@freemail.hu> added the comment:

Can you review my final implementation?
https://github.com/python/cpython/pull/28111

----------
versions: +Python 3.11

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:


New changeset 85a4748118c3793be7047ecbcbfc79dd07cb2a75 by Livius in branch 'main':
bpo-21302: Add clock_nanosleep() implementation for time.sleep() (GH-28111)
https://github.com/python/cpython/commit/85a4748118c3793be7047ecbcbfc79dd07cb2a75


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Change by STINNER Victor <vstinner@python.org>:


----------
pull_requests: +26724
pull_request: https://github.com/python/cpython/pull/28311

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:


New changeset 85dc53a463967659075744ad911d08a32aa70dd5 by Victor Stinner in branch 'main':
bpo-21302: Update time.sleep() doc for clock_nanosleep() (GH-28311)
https://github.com/python/cpython/commit/85dc53a463967659075744ad911d08a32aa70dd5


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Change by Benjamin Sz?ke <egyszeregy@freemail.hu>:


----------
pull_requests: +26754
pull_request: https://github.com/python/cpython/pull/28341

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Change by STINNER Victor <vstinner@python.org>:


----------
pull_requests: +26764
pull_request: https://github.com/python/cpython/pull/28350

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:


New changeset b49263b698993cad2b8aaddc55cdeaa678412b30 by Victor Stinner in branch 'main':
bpo-21302: Add _PyTime_AsNanoseconds() (GH-28350)
https://github.com/python/cpython/commit/b49263b698993cad2b8aaddc55cdeaa678412b30


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

wait.py: script to test the time.sleep() function. Press CTRL+C multiple times during the sleep to check that even if the sleep is interrupted, time.sleep() sleeps the expected duration.

----------
Added file: https://bugs.python.org/file50289/wait.py

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Change by STINNER Victor <vstinner@python.org>:


----------
pull_requests: +26882
pull_request: https://github.com/python/cpython/pull/28483

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:


New changeset 58f8adfda3c2b42f654a55500e8e3a6433cb95f2 by Victor Stinner in branch 'main':
bpo-21302: time.sleep() uses waitable timer on Windows (GH-28483)
https://github.com/python/cpython/commit/58f8adfda3c2b42f654a55500e8e3a6433cb95f2


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

Livius: your first PR modified Sleep() in Modules/_tkinter.c to use nanosleep(). I don't see the point since this function has a solution of 1 ms (10^-3). Using select() on Unix is enough: resolution of 1 us (10^-6).

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

bench.py: measure the shortest possible sleep. Use time.sleep(1e-10): 0.1 nanosecond. It should be rounded to the resolution of the used sleep function, like 1 ns on Linux.

On Linux with Fedora 34 Python 3.10 executable, I get:

Mean +- std dev: 60.5 us +- 12.9 us (80783 values)

On Windows with a Python 3.11 debug build, I get:

Mean +- std dev: 21.9 ms +- 7.8 ms (228 values)

Sadly, it seems like on Windows 10, one of the following function still uses the infamous 15.6 ms resolution:

* CreateWaitableTimerW()
* SetWaitableTimer()
* WaitForMultipleObjects()

----------
Added file: https://bugs.python.org/file50294/bench.py

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

> On Windows with a Python 3.11 debug build, I get:
> Mean +- std dev: 21.9 ms +- 7.8 ms (228 values)

I wrote an optimization to cache the Windows timer handle between time.sleep() calls (don't close it). I don't think that it's needed because they shortest sleep is about 15.6 ms. CreateWaitableTimerW() is likely way more fast than 15.6 ms. So this optimization is basically useless.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

Livius: do you care about using nanosleep(), or can I close the issue?

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

See also bpo-19007: "precise time.time() under Windows 8: use GetSystemTimePreciseAsFileTime".

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Change by Benjamin Sz?ke <egyszeregy@freemail.hu>:


----------
pull_requests: +26917
pull_request: https://github.com/python/cpython/pull/28526

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Change by STINNER Victor <vstinner@python.org>:


----------
pull_requests: +26929
pull_request: https://github.com/python/cpython/pull/28545

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:


New changeset 7834ff26cbcd4d8394d64d80d9f51a364d38b1c6 by Victor Stinner in branch 'main':
bpo-21302: Add nanosleep() implementation for time.sleep() in Unix (GH-28545)
https://github.com/python/cpython/commit/7834ff26cbcd4d8394d64d80d9f51a364d38b1c6


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

Thanks Livius for all these nice enhancements!

----------
resolution: -> fixed
stage: patch review -> resolved
status: open -> closed

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Benjamin Sz?ke <egyszeregy@freemail.hu> added the comment:

Do you have any information about when will be it released in 3.11?

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

> Do you have any information about when will be it released in 3.11?

Here is a schedule of Python 3.11 releases:
https://www.python.org/dev/peps/pep-0664/

In the meanwhile, you can develop a C extension to get the feature.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Benjamin Sz?ke <egyszeregy@freemail.hu> added the comment:

https://www.python.org/downloads/windows/
"Note that Python 3.10.0 cannot be used on Windows 7 or earlier."

vstinner: Is it true that Windows 7 is not supported OS anymore? In this case we do not need to care about Windows 7 and earlier Windows OS compatibility and it is time to use nicely GetSystemTimePreciseAsFileTime() in time.time() and time.sleep() as absolute sleeping because it is available since Windows 8.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

> it is time to use nicely GetSystemTimePreciseAsFileTime() in time.time()

See bpo-19007 for that.

> it is time to (...) time.sleep() as absolute sleeping because it is available since Windows 8.

In Python 3.11, time.sleep() is now always implemented with a waitable timer. I chose to use a relative timeout since it's simpler to implement. Is there any benefit of calling SetWaitableTimer() with an absolute timeout, compared to calling it with a relative timeout?

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Eryk Sun <eryksun@gmail.com> added the comment:

> In Python 3.11, time.sleep() is now always implemented with a
> waitable timer.

A regular waitable timer in Windows becomes signaled with the same resolution as Sleep(). It's based on the current interrupt timer period, which can be lowered to 1 ms via timeBeginPeriod(). Compared to Sleep() it's more flexible in terms of periodic waits, WaitForMultipleObjects(), or MsgWaitForMultipleObjects() -- not that time.sleep() needs this flexibility.

That said, using a waitable timer leaves the door open for improvement in future versions of Python. In particular, it's possible to get higher resolution in newer versions of Windows 10 and Windows 11 with CreateWaitableTimerExW() and the undocumented flag CREATE_WAITABLE_TIMER_HIGH_RESOLUTION (2).

----------
nosy: +eryksun

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Benjamin Sz?ke <egyszeregy@freemail.hu> added the comment:

Absolute timeout implementation via SetWaitableTimer() and GetSystemTimePreciseAsFileTime() is always better because it can reduce the "waste time" or "overhead time" what is always exist in any simple interval sleep implementation. Moreover, it is the only one which is eqvivalent with clock_nanosleep() implementation of Linux which is now the most state of the art implementation for precise sleeping.

So, my opinion is that absolute timeout implementation could be the most modern and sustainable for future python.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Eryk Sun <eryksun@gmail.com> added the comment:

> Is there any benefit of calling SetWaitableTimer() with an
> absolute timeout

No, the due time of a timer object is stored in absolute interrupt time, not absolute system time. This has to be calculated either way, and it's actually more work for the kernel if an absolute system time is passed.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Benjamin Sz?ke <egyszeregy@freemail.hu> added the comment:

It is not true that there are no benefits. Absolute timeout using can reduce the overhead time of any variable and object intialization cost before the WaitForMultipleObjects() which will perform the real sleeping via blocking wait in pysleep(). GetSystemTimePreciseAsFileTime() must be call at the first line as much as it can in pysleep(). This is the same implementation in Linux via clock_nanosleep().

So, to using absolute timeout and GetSystemTimePreciseAsFileTime() can improves the accuracy of the desired sleep time. For example if sleep = 2.0 sec then real relative sleep time = 2.001234 sec, but absolute sleep time = 2.000012 sec.

Benefits are in not the technicaly backgorund, rather it is in the usecase.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Benjamin Sz?ke <egyszeregy@freemail.hu> added the comment:

In other words, using absolute timeout can eliminate the systematic error of desired sleep time.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
Eryk Sun <eryksun@gmail.com> added the comment:

> Absolute timeout using can reduce the overhead time of any variable
> and object intialization cost before the WaitForMultipleObjects()

Again, timer objects store the due time in interrupt time, not system time (i.e. InterruptTime vs SystemTime in the KUSER_SHARED_DATA record). The due time gets set as the current interrupt time plus a relative due time. If the due time is passed as absolute system time, the kernel just computes the delta from the current system time.

The timer object does record whether the requested due time is an absolute system time. This allows the kernel to recompute all absolute due times when the system time is changed manually. This is also the primary reason one wouldn't implement time.sleep() with absolute system time.

> using absolute timeout and GetSystemTimePreciseAsFileTime() can
> improves the accuracy of the desired sleep time.

It would not improve the resolution. Timer objects are signaled when their due time is at or before the current interrupt time. The latter gets updated by the timer interrupt service routine, by default every 15.625 ms -- or at least that used to be the case.

The undocumented flag CREATE_WAITABLE_TIMER_HIGH_RESOLUTION creates a different timer type, called an "IRTimer" (implemented in Windows 8.1, but back then only accessible in the NT API). This timer type is based on precise interrupt time, which is interpolated using the performance counter. I don't know how the implementation of the timer interrupt has changed to support this increased resolution. It could be that the default 15.625 ms interrupt period is being simulated for compatibility with classic timers and Sleep(). I'd love for the CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag and the behavior of IRTimer objects to be documented.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

> That said, using a waitable timer leaves the door open for improvement in future versions of Python. In particular, it's possible to get higher resolution in newer versions of Windows 10 and Windows 11 with CreateWaitableTimerExW() and the undocumented flag CREATE_WAITABLE_TIMER_HIGH_RESOLUTION (2).

I created bpo-45429 "[Windows] time.sleep() should use CREATE_WAITABLE_TIMER_HIGH_RESOLUTION".

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue21302] time.sleep (floatsleep()) should use clock_nanosleep() on Linux [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

This issue is closed. If you consider that time.sleep() has a bug or could be enhanced, please open a new issue.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue21302>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com