Skip to content

Commit ae58649

Browse files
committed
Issue #22043: time.monotonic() is now always available
threading.Lock.acquire(), threading.RLock.acquire() and socket operations now use a monotonic clock, instead of the system clock, when a timeout is used.
1 parent 9bb758c commit ae58649

File tree

17 files changed

+226
-176
lines changed

17 files changed

+226
-176
lines changed

Doc/library/time.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,9 @@ The module defines the following functions and data items:
315315
processes running for more than 49 days. On more recent versions of Windows
316316
and on other operating systems, :func:`monotonic` is system-wide.
317317

318-
Availability: Windows, Mac OS X, Linux, FreeBSD, OpenBSD, Solaris.
319-
320318
.. versionadded:: 3.3
319+
.. versionchanged:: 3.5
320+
The function is now always available.
321321

322322

323323
.. function:: perf_counter()

Doc/whatsnew/3.5.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ socket
238238
:meth:`socket.socket.send`.
239239
(contributed by Giampaolo Rodola' in :issue:`17552`)
240240

241+
time
242+
----
243+
244+
The :func:`time.monotonic` function is now always available (:issue`22043`).
245+
241246
wsgiref
242247
-------
243248

Include/pytime.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
9191
long *nsec,
9292
_PyTime_round_t);
9393

94+
/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
95+
The clock is not affected by system clock updates. The reference point of
96+
the returned value is undefined, so that only the difference between the
97+
results of consecutive calls is valid.
98+
99+
The function never fails. _PyTime_Init() ensures that a monotonic clock
100+
is available and works. */
101+
PyAPI_FUNC(void) _PyTime_monotonic(
102+
_PyTime_timeval *tp);
103+
104+
/* Similar to _PyTime_monotonic(), fill also info (if set) with information of
105+
the function used to get the time.
106+
107+
Return 0 on success, raise an exception and return -1 on error. */
108+
PyAPI_FUNC(int) _PyTime_monotonic_info(
109+
_PyTime_timeval *tp,
110+
_Py_clock_info_t *info);
111+
94112
/* Initialize time.
95113
Return 0 on success, raise an exception and return -1 on error. */
96114
PyAPI_FUNC(int) _PyTime_Init(void);

Lib/queue.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import dummy_threading as threading
77
from collections import deque
88
from heapq import heappush, heappop
9-
try:
10-
from time import monotonic as time
11-
except ImportError:
12-
from time import time
9+
from time import monotonic as time
1310

1411
__all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue']
1512

Lib/sched.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@
3535
import threading
3636
except ImportError:
3737
import dummy_threading as threading
38-
try:
39-
from time import monotonic as _time
40-
except ImportError:
41-
from time import time as _time
38+
from time import monotonic as _time
4239

4340
__all__ = ["scheduler"]
4441

Lib/socketserver.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ class will essentially render the service "deaf" while one request is
136136
import threading
137137
except ImportError:
138138
import dummy_threading as threading
139-
try:
140-
from time import monotonic as time
141-
except ImportError:
142-
from time import time as time
139+
from time import monotonic as time
143140

144141
__all__ = ["TCPServer","UDPServer","ForkingUDPServer","ForkingTCPServer",
145142
"ThreadingUDPServer","ThreadingTCPServer","BaseRequestHandler",

Lib/subprocess.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,7 @@ class Popen(args, bufsize=-1, executable=None,
365365
import builtins
366366
import warnings
367367
import errno
368-
try:
369-
from time import monotonic as _time
370-
except ImportError:
371-
from time import time as _time
368+
from time import monotonic as _time
372369

373370
# Exception classes used by this module.
374371
class SubprocessError(Exception): pass

Lib/telnetlib.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
import sys
3737
import socket
3838
import selectors
39-
try:
40-
from time import monotonic as _time
41-
except ImportError:
42-
from time import time as _time
39+
from time import monotonic as _time
4340

4441
__all__ = ["Telnet"]
4542

Lib/test/test_selectors.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
from time import sleep
99
import unittest
1010
import unittest.mock
11-
try:
12-
from time import monotonic as time
13-
except ImportError:
14-
from time import time as time
11+
from time import monotonic as time
1512
try:
1613
import resource
1714
except ImportError:

Lib/threading.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import sys as _sys
44
import _thread
55

6-
try:
7-
from time import monotonic as _time
8-
except ImportError:
9-
from time import time as _time
6+
from time import monotonic as _time
107
from traceback import format_exc as _format_exc
118
from _weakrefset import WeakSet
129
from itertools import islice as _islice

0 commit comments

Comments
 (0)