https://github.com/python/cpython/commit/3d9b14c19bb004619680bd972f641486e1fec1b7
commit: 3d9b14c19bb004619680bd972f641486e1fec1b7
branch: 3.12
author: Bénédikt Tran <[email protected]>
committer: picnixz <[email protected]>
date: 2025-01-18T11:13:03Z
summary:

[3.12] gh-125997: suggest efficient alternatives for `time.sleep(0)` 
(GH-128752) (#128985)

files:
M Doc/library/os.rst
M Doc/library/time.rst

diff --git a/Doc/library/os.rst b/Doc/library/os.rst
index 18e58249a81b08..0c53e4bdc1c694 100644
--- a/Doc/library/os.rst
+++ b/Doc/library/os.rst
@@ -5058,6 +5058,8 @@ information, consult your Unix manpages.
 The following scheduling policies are exposed if they are supported by the
 operating system.
 
+.. _os-scheduling-policy:
+
 .. data:: SCHED_OTHER
 
    The default scheduling policy.
@@ -5149,7 +5151,7 @@ operating system.
 
 .. function:: sched_yield()
 
-   Voluntarily relinquish the CPU.
+   Voluntarily relinquish the CPU. See :manpage:`sched_yield(2)` for details.
 
 
 .. function:: sched_setaffinity(pid, mask, /)
diff --git a/Doc/library/time.rst b/Doc/library/time.rst
index db53296e2a5698..ebc1ef1abac23b 100644
--- a/Doc/library/time.rst
+++ b/Doc/library/time.rst
@@ -367,6 +367,8 @@ Functions
    The suspension time may be longer than requested by an arbitrary amount,
    because of the scheduling of other activity in the system.
 
+   .. rubric:: Windows implementation
+
    On Windows, if *secs* is zero, the thread relinquishes 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
@@ -375,12 +377,19 @@ Functions
    
<https://learn.microsoft.com/windows-hardware/drivers/kernel/high-resolution-timers>`_
    which provides resolution of 100 nanoseconds. If *secs* is zero, 
``Sleep(0)`` is used.
 
-   Unix implementation:
+   .. rubric:: Unix implementation
 
    * Use ``clock_nanosleep()`` if available (resolution: 1 nanosecond);
    * Or use ``nanosleep()`` if available (resolution: 1 nanosecond);
    * Or use ``select()`` (resolution: 1 microsecond).
 
+   .. note::
+
+      To emulate a "no-op", use :keyword:`pass` instead of ``time.sleep(0)``.
+
+      To voluntarily relinquish the CPU, specify a real-time :ref:`scheduling
+      policy <os-scheduling-policy>` and use :func:`os.sched_yield` instead.
+
    .. versionchanged:: 3.5
       The function now sleeps at least *secs* even if the sleep is interrupted
       by a signal, except if the signal handler raises an exception (see

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to