Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-125997: ensure that time.sleep(0) is not delayed on non-Windows platforms#128274
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
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3754e9e
Ensure that `time.sleep(0)` does not accumulate delays.
picnixz 3c0c4e6
update globals
picnixz c18f15f
blurb
picnixz 8d270f2
fix Windows compilation
picnixz 2996937
use a fresh `timeval` variable for select(2) portability
picnixz 80de853
ensure that `errno=0` when calling `pysleep[zero_posix]`
picnixz c7aa428
removing `assert(errno == 0)`
picnixz f15436e
add errors assertions
picnixz 68e2988
simplify code
picnixz afc6186
add comments for `time.sleep(0)`
picnixz f6a817f
improve time.sleep() tests
picnixz fb9eb41
update `os` scheduling policy docs
picnixz ecb9636
update docs
picnixz 1ed9877
add What's New
picnixz 23b4740
fix typo
picnixz 40155ff
optimize `time.sleep(0)` path
picnixz 153b326
update docs
picnixz 7e351c9
update tests
picnixz 285c54a
revert un-necessary docs updates
picnixz 434da31
remove performance tests
picnixz ff06516
implement platform compile-time dependent `time.sleep(0)`
picnixz 40d56f1
address Victor's review
picnixz c4ce9cc
update blurb
picnixz 54b6dde
simplify `pysleep`
picnixz 405e473
add newline for readability
picnixz b803045
revert to a `select()` alternative
picnixz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4 Misc/NEWS.d/next/Library/2024-12-26-13-09-25.gh-issue-125997.Vsibep.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Specialize :func:`time.sleep(0) <time.sleep>` on non-Windows platforms to | ||
| always use :manpage:`select(2)` even if the :manpage:`clock_nanosleep` or | ||
| :manpage:`nanosleep` functions are present as these functions would sleep | ||
| for much longer than what is actually needed. Patch by Bénédikt Tran. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -72,6 +72,7 @@ module time | ||
| /* Forward declarations */ | ||
| static int pysleep(PyTime_t timeout); | ||
| static int pysleep_zero(void); // see gh-125997 | ||
| typedef struct { | ||
| @@ -2213,6 +2214,10 @@ static int | ||
| pysleep(PyTime_t timeout) | ||
| { | ||
| assert(timeout >= 0); | ||
| assert(!PyErr_Occurred()); | ||
| if (timeout == 0) { // gh-125997 | ||
| return pysleep_zero(); | ||
| } | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| #ifndef MS_WINDOWS | ||
| #ifdef HAVE_CLOCK_NANOSLEEP | ||
| @@ -2292,20 +2297,8 @@ pysleep(PyTime_t timeout) | ||
| return 0; | ||
| #else // MS_WINDOWS | ||
| PyTime_t timeout_100ns = _PyTime_As100Nanoseconds(timeout, | ||
| _PyTime_ROUND_CEILING); | ||
| // Maintain Windows Sleep() semantics for time.sleep(0) | ||
| if (timeout_100ns == 0) { | ||
| Py_BEGIN_ALLOW_THREADS | ||
| // A value of zero causes the thread to relinquish the remainder of its | ||
| // time slice to any other thread that is ready to run. If there are no | ||
| // other threads ready to run, the function returns immediately, and | ||
| // the thread continues execution. | ||
| Sleep(0); | ||
| Py_END_ALLOW_THREADS | ||
| return 0; | ||
| } | ||
| _PyTime_ROUND_CEILING); | ||
| assert(timeout_100ns > 0); | ||
| LARGE_INTEGER relative_timeout; | ||
| // No need to check for integer overflow, both types are signed | ||
| assert(sizeof(relative_timeout) == sizeof(timeout_100ns)); | ||
| @@ -2390,3 +2383,57 @@ pysleep(PyTime_t timeout) | ||
| return -1; | ||
| #endif | ||
| } | ||
| // time.sleep(0) optimized implementation. | ||
| // On error, raise an exception and return -1. | ||
| // On success, return 0. | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // | ||
| // Rationale | ||
| // --------- | ||
| // time.sleep(0) is slower when using the generic implementation, but we make | ||
| // it faster than time.sleep(eps) for eps > 0 so to avoid some performance | ||
| // annoyance. For details, see https://github.com/python/cpython/pull/128274. | ||
| static int | ||
| pysleep_zero(void) | ||
| { | ||
| assert(!PyErr_Occurred()); | ||
| #ifndef MS_WINDOWS | ||
| int ret, err; | ||
| Py_BEGIN_ALLOW_THREADS | ||
| // POSIX-compliant select(2) allows the 'timeout' parameter to | ||
| // be modified but also mandates that the function should return | ||
| // immediately if *both* structure's fields are zero (which is | ||
| // the case here). | ||
| // | ||
| // However, since System V (but not BSD) variant typically sets | ||
| // the timeout before returning (but does not specify whether | ||
| // this is also the case for zero timeouts), we prefer supplying | ||
| // a fresh timeout everytime. | ||
| struct timeval zero = {0, 0}; | ||
| ret = select(0, NULL, NULL, NULL, &zero); | ||
| err = errno; | ||
| Py_END_ALLOW_THREADS | ||
| if (ret == 0) { | ||
| return 0; | ||
| } | ||
| if (err != EINTR) { | ||
| errno = err; | ||
| PyErr_SetFromErrno(PyExc_OSError); | ||
| return -1; | ||
| } | ||
| /* sleep was interrupted by SIGINT */ | ||
| if (PyErr_CheckSignals()) { | ||
| return -1; | ||
| } | ||
| #else // Windows implementation | ||
| Py_BEGIN_ALLOW_THREADS | ||
| // A value of zero causes the thread to relinquish the remainder of its | ||
| // time slice to any other thread that is ready to run. If there are no | ||
| // other threads ready to run, the function returns immediately, and | ||
| // the thread continues execution. | ||
| Sleep(0); | ||
| Py_END_ALLOW_THREADS | ||
| #endif | ||
| return 0; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.