From 972458a4245afb4e934fbcb9c0224b105fb5d7ba Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Fri, 6 Jul 2018 05:14:33 -0700 Subject: [PATCH] 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. (cherry picked from commit c2368cbc83ca2bafeaea0e4760be4996046d0444) Co-authored-by: Victor Stinner --- Lib/multiprocessing/connection.py | 8 ++++---- Lib/multiprocessing/managers.py | 6 +++--- Lib/multiprocessing/queues.py | 4 ++-- Lib/multiprocessing/synchronize.py | 7 +++---- .../next/Library/2018-07-05-18-37-05.bpo-34054.nWRS6M.rst | 3 +++ 5 files changed, 15 insertions(+), 13 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-07-05-18-37-05.bpo-34054.nWRS6M.rst diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py index d49e8f0d32b62a..d3797503a7551e 100644 --- a/Lib/multiprocessing/connection.py +++ b/Lib/multiprocessing/connection.py @@ -57,10 +57,10 @@ def _init_timeout(timeout=CONNECTION_TIMEOUT): - return time.time() + timeout + return time.monotonic() + timeout def _check_timeout(t): - return time.time() > t + return time.monotonic() > t # # @@ -905,7 +905,7 @@ def wait(object_list, timeout=None): selector.register(obj, selectors.EVENT_READ) if timeout is not None: - deadline = time.time() + timeout + deadline = time.monotonic() + timeout while True: ready = selector.select(timeout) @@ -913,7 +913,7 @@ def wait(object_list, timeout=None): return [key.fileobj for (key, events) in ready] else: if timeout is not None: - timeout = deadline - time.time() + timeout = deadline - time.monotonic() if timeout < 0: return ready diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py index b9ce84b2d85ddc..27f26fdf6fecff 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -18,8 +18,8 @@ import threading import array import queue +import time -from time import time as _time from traceback import format_exc from . import connection @@ -1006,13 +1006,13 @@ def wait_for(self, predicate, timeout=None): if result: return result if timeout is not None: - endtime = _time() + timeout + endtime = time.monotonic() + timeout else: endtime = None waittime = None while not result: if endtime is not None: - waittime = endtime - _time() + waittime = endtime - time.monotonic() if waittime <= 0: break self.wait(waittime) diff --git a/Lib/multiprocessing/queues.py b/Lib/multiprocessing/queues.py index 513807cafecb6d..1b3fddb2bfece7 100644 --- a/Lib/multiprocessing/queues.py +++ b/Lib/multiprocessing/queues.py @@ -95,12 +95,12 @@ def get(self, block=True, timeout=None): self._sem.release() else: if block: - deadline = time.time() + timeout + deadline = time.monotonic() + timeout if not self._rlock.acquire(block, timeout): raise Empty try: if block: - timeout = deadline - time.time() + timeout = deadline - time.monotonic() if not self._poll(timeout): raise Empty elif not self._poll(): diff --git a/Lib/multiprocessing/synchronize.py b/Lib/multiprocessing/synchronize.py index d4bdf0e8b1737f..364adbe6087856 100644 --- a/Lib/multiprocessing/synchronize.py +++ b/Lib/multiprocessing/synchronize.py @@ -15,8 +15,7 @@ import sys import tempfile import _multiprocessing - -from time import time as _time +import time from . import context from . import process @@ -313,13 +312,13 @@ def wait_for(self, predicate, timeout=None): if result: return result if timeout is not None: - endtime = _time() + timeout + endtime = time.monotonic() + timeout else: endtime = None waittime = None while not result: if endtime is not None: - waittime = endtime - _time() + waittime = endtime - time.monotonic() if waittime <= 0: break self.wait(waittime) diff --git a/Misc/NEWS.d/next/Library/2018-07-05-18-37-05.bpo-34054.nWRS6M.rst b/Misc/NEWS.d/next/Library/2018-07-05-18-37-05.bpo-34054.nWRS6M.rst new file mode 100644 index 00000000000000..9d4d1f24db496f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-07-05-18-37-05.bpo-34054.nWRS6M.rst @@ -0,0 +1,3 @@ +The multiprocessing module now uses the monotonic clock +:func:`time.monotonic` instead of the system clock :func:`time.time` to +implement timeout.