Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address Victor's review
  • Loading branch information
picnixz committed Jan 7, 2025
commit 40d56f1d376b279bc7f14af6ebdf5c6113b660a5
5 changes: 3 additions & 2 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,9 @@ Two new events are added: :monitoring-event:`BRANCH_LEFT` and
time
----

* Ensure that :func:`time.sleep(0) <time.sleep>` does not degrade over time
on non-Windows platforms.
* Ensure that the duration of :func:`time.sleep(0) <time.sleep>` is as small
as possible on non-Windows platforms when :manpage:`clock_nanosleep(2)`
or :manpage:`nanosleep(2)` are used to implement :func:`!time.sleep`.
(Contributed by Bénédikt Tran in :gh:`125997`.)


Expand Down
11 changes: 5 additions & 6 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2222,8 +2222,7 @@ pysleep(PyTime_t timeout)
#else
PyTime_t timeout_100ns = _PyTime_As100Nanoseconds(timeout,
_PyTime_ROUND_CEILING);
// Maintain Windows Sleep() semantics for time.sleep(0)
if (timeout_100ns == 0) {
if (timeout_100ns == 0) { // gh-125997
return pysleep_zero();
}
#endif
Expand Down Expand Up @@ -2397,9 +2396,9 @@ pysleep(PyTime_t timeout)
//
// Rationale
// ---------
// time.sleep(0) accumulates delays in the generic implementation, but we can
// skip some calls to `PyTime_Monotonic()` and other checks when the timeout
// is zero. For details, see https://github.com/python/cpython/pull/128274.
// 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)
{
Expand Down Expand Up @@ -2442,7 +2441,7 @@ pysleep_zero(void)
if (PyErr_CheckSignals()) {
return -1;
}
#else
#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
Expand Down