Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3d9b14c

Browse filesBrowse files
authored
[3.12] gh-125997: suggest efficient alternatives for time.sleep(0) (GH-128752) (#128985)
1 parent fbbef60 commit 3d9b14c
Copy full SHA for 3d9b14c

File tree

Expand file treeCollapse file tree

2 files changed

+13
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+13
-2
lines changed

‎Doc/library/os.rst

Copy file name to clipboardExpand all lines: Doc/library/os.rst
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5058,6 +5058,8 @@ information, consult your Unix manpages.
50585058
The following scheduling policies are exposed if they are supported by the
50595059
operating system.
50605060

5061+
.. _os-scheduling-policy:
5062+
50615063
.. data:: SCHED_OTHER
50625064

50635065
The default scheduling policy.
@@ -5149,7 +5151,7 @@ operating system.
51495151

51505152
.. function:: sched_yield()
51515153

5152-
Voluntarily relinquish the CPU.
5154+
Voluntarily relinquish the CPU. See :manpage:`sched_yield(2)` for details.
51535155

51545156

51555157
.. function:: sched_setaffinity(pid, mask, /)

‎Doc/library/time.rst

Copy file name to clipboardExpand all lines: Doc/library/time.rst
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ Functions
367367
The suspension time may be longer than requested by an arbitrary amount,
368368
because of the scheduling of other activity in the system.
369369

370+
.. rubric:: Windows implementation
371+
370372
On Windows, if *secs* is zero, the thread relinquishes the remainder of its
371373
time slice to any other thread that is ready to run. If there are no other
372374
threads ready to run, the function returns immediately, and the thread
@@ -375,12 +377,19 @@ Functions
375377
<https://learn.microsoft.com/windows-hardware/drivers/kernel/high-resolution-timers>`_
376378
which provides resolution of 100 nanoseconds. If *secs* is zero, ``Sleep(0)`` is used.
377379

378-
Unix implementation:
380+
.. rubric:: Unix implementation
379381

380382
* Use ``clock_nanosleep()`` if available (resolution: 1 nanosecond);
381383
* Or use ``nanosleep()`` if available (resolution: 1 nanosecond);
382384
* Or use ``select()`` (resolution: 1 microsecond).
383385

386+
.. note::
387+
388+
To emulate a "no-op", use :keyword:`pass` instead of ``time.sleep(0)``.
389+
390+
To voluntarily relinquish the CPU, specify a real-time :ref:`scheduling
391+
policy <os-scheduling-policy>` and use :func:`os.sched_yield` instead.
392+
384393
.. versionchanged:: 3.5
385394
The function now sleeps at least *secs* even if the sleep is interrupted
386395
by a signal, except if the signal handler raises an exception (see

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.