Skip to content

Commit c2368cb

Browse files
authored
bpo-34054: multiprocessing uses time.monotonic() (GH-8118)
The multiprocessing module now uses the monotonic clock time.monotonic() instead of the system clock time.time() to implement timeouts.
1 parent 6f19fc6 commit c2368cb

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

Lib/multiprocessing/connection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@
5757

5858

5959
def _init_timeout(timeout=CONNECTION_TIMEOUT):
60-
return time.time() + timeout
60+
return time.monotonic() + timeout
6161

6262
def _check_timeout(t):
63-
return time.time() > t
63+
return time.monotonic() > t
6464

6565
#
6666
#
@@ -914,15 +914,15 @@ def wait(object_list, timeout=None):
914914
selector.register(obj, selectors.EVENT_READ)
915915

916916
if timeout is not None:
917-
deadline = time.time() + timeout
917+
deadline = time.monotonic() + timeout
918918

919919
while True:
920920
ready = selector.select(timeout)
921921
if ready:
922922
return [key.fileobj for (key, events) in ready]
923923
else:
924924
if timeout is not None:
925-
timeout = deadline - time.time()
925+
timeout = deadline - time.monotonic()
926926
if timeout < 0:
927927
return ready
928928

Lib/multiprocessing/managers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import threading
1919
import array
2020
import queue
21+
import time
2122

22-
from time import time as _time
2323
from traceback import format_exc
2424

2525
from . import connection
@@ -1045,13 +1045,13 @@ def wait_for(self, predicate, timeout=None):
10451045
if result:
10461046
return result
10471047
if timeout is not None:
1048-
endtime = _time() + timeout
1048+
endtime = time.monotonic() + timeout
10491049
else:
10501050
endtime = None
10511051
waittime = None
10521052
while not result:
10531053
if endtime is not None:
1054-
waittime = endtime - _time()
1054+
waittime = endtime - time.monotonic()
10551055
if waittime <= 0:
10561056
break
10571057
self.wait(waittime)

Lib/multiprocessing/queues.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ def get(self, block=True, timeout=None):
9595
self._sem.release()
9696
else:
9797
if block:
98-
deadline = time.time() + timeout
98+
deadline = time.monotonic() + timeout
9999
if not self._rlock.acquire(block, timeout):
100100
raise Empty
101101
try:
102102
if block:
103-
timeout = deadline - time.time()
103+
timeout = deadline - time.monotonic()
104104
if not self._poll(timeout):
105105
raise Empty
106106
elif not self._poll():

Lib/multiprocessing/synchronize.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
import sys
1616
import tempfile
1717
import _multiprocessing
18-
19-
from time import time as _time
18+
import time
2019

2120
from . import context
2221
from . import process
@@ -302,13 +301,13 @@ def wait_for(self, predicate, timeout=None):
302301
if result:
303302
return result
304303
if timeout is not None:
305-
endtime = _time() + timeout
304+
endtime = time.monotonic() + timeout
306305
else:
307306
endtime = None
308307
waittime = None
309308
while not result:
310309
if endtime is not None:
311-
waittime = endtime - _time()
310+
waittime = endtime - time.monotonic()
312311
if waittime <= 0:
313312
break
314313
self.wait(waittime)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The multiprocessing module now uses the monotonic clock
2+
:func:`time.monotonic` instead of the system clock :func:`time.time` to
3+
implement timeout.

0 commit comments

Comments
 (0)