-
-
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
bpo-21302: Add clock_nanosleep() implementation for time.sleep() #28111
Conversation
clock_nanosleep() is available in Linux which has POSIX 2001.12 or newer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice, it's much better!
IMO it's worth to mention that time.sleep() has a resolution of 1 nanosecond if the clock_nanosleep() function is available.
Do you know if clock_nanosleep() is available on other platforms than Linux? Is it available on macOS or FreeBSD for example?
I do not think that it is available in macOS and FreeBSD according the gnulib manual, clock_nanosleep() is available sure in Linux from POSIX version 2001.12. So any modern and new Linux based OS has it, moreover it seems to me QNX has also the same clock_nanosleep(). Is there any right place to mention this improved resolution of time.sleep()? |
PEP 7 rules appled for _PyTime_AsTimespec and _PyTime_AsTimeval error checking in pysleep().
In calling clock_nanosleep() EINTR is not stored in errno. Need to use return value of clock_nanosleep()/select() for checking it.
Unix operating systems eg: Linux, macOS, FreeBSD etc. time.sleep() has a resolution of nanoseconds with using clock_nanosleep() or nanosleep() function.
In all Unix systems eg: macOS, FreeBSD, Linux etc, nanosleep() is available and as i see there are some codes where select() is used for sleeping microsecs resolution, semaphore.c, _tkinter.c. Sleeping via select() was the state of the art solution in about 1998 in Linux but it is already an outdated solution nowadays. It would be better to use nanosleep() in these codes also. I will improve it in the rest of codes, too. |
In all Unix systems eg: macOS, FreeBSD, Linux etc, nanosleep() is available.
Waitable timer is 100 nsec resolution. Now, seconds to nanosec conversion is limited in usec, soon will come the next developing part to improve it in next commit.
Waitable timer resolution is 100 nsec but it is limited to 1 usec by round ceiling, moreover sleep for lower then 1 milisec is not possible in Win32 API.
Improved time.sleep() is ready for final review. All platforms have now the state of the art solution for sleeping. In Windows it uses Waitable timer object, in Linux it is clock_nanosleep() and in other Unix systems eg: macOS, FreeBSD it is nanosleep() function. Resolution of Waitable timer object is 100 nsec but it is limited to 1 usec by round ceiling. However, sleeping for lower than 1 millisec is not possible in Win32 API because the overhead of the the event waiting call is more than nanoseconds. Resolution of clock_nanosleep() and nanosleep() is 1 nsec, clock_nanosleep() is suitable for blocking/waiting until an absolute time date, nanosleep() is suitable only for interval sleeping. @abalkin, @pganssle and @vstinner please make a review for my final state. |
Can not simplify HAVE_CLOCK_NANOSLEEP blocks because do {} while loop need for EINTR error handling
Merged, thanks! I created PR #28311 follow-up to update the documentation. |
Thanks the merging. What are the formal rules to create a new PR to improve Windows implementation, too? Is it good to create a new PR without any bpo issue number? Or can i make more PR (windows implementation and nanosleep()) for this bpo-21302? |
In this case, you can reuse bpo-21302. |
Requested changes from @vstinner are done. It was runt-time tested in Oracle Linux 8.4 and Windows 10.
Some previous pull request history can be found here: #28077
https://bugs.python.org/issue21302