]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-34819: Use a monotonic clock to compute timeouts in concurrent.futures (GH-9599)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 27 Sep 2018 11:37:33 +0000 (04:37 -0700)
committerGitHub <noreply@github.com>
Thu, 27 Sep 2018 11:37:33 +0000 (04:37 -0700)
Use a monotonic clock to compute timeouts in :meth:`Executor.map` and :func:`as_completed`, in order to prevent timeouts from deviating when the system clock is adjusted.

This may not be sufficient on all systems. On POSIX for example, the actual waiting (e.g. in ``sem_timedwait``) is specified to rely on the CLOCK_REALTIME clock.
(cherry picked from commit a94ee12c26aa8dd7dce01373779df8055aff765b)

Co-authored-by: orlnub123 <orlnub123@gmail.com>
Lib/concurrent/futures/_base.py
Misc/NEWS.d/next/Library/2018-09-27-09-45-00.bpo-34819.9ZaFyO.rst [new file with mode: 0644]

index 6bace6c7464121c5869c7fee880ad5dcd02ece02..4c140508c9eaec1f3883b90c9adfb06a2c61dbab 100644 (file)
@@ -212,7 +212,7 @@ def as_completed(fs, timeout=None):
             before the given timeout.
     """
     if timeout is not None:
-        end_time = timeout + time.time()
+        end_time = timeout + time.monotonic()
 
     fs = set(fs)
     total_futures = len(fs)
@@ -231,7 +231,7 @@ def as_completed(fs, timeout=None):
             if timeout is None:
                 wait_timeout = None
             else:
-                wait_timeout = end_time - time.time()
+                wait_timeout = end_time - time.monotonic()
                 if wait_timeout < 0:
                     raise TimeoutError(
                             '%d (of %d) futures unfinished' % (
@@ -570,7 +570,7 @@ class Executor(object):
             Exception: If fn(*args) raises for any values.
         """
         if timeout is not None:
-            end_time = timeout + time.time()
+            end_time = timeout + time.monotonic()
 
         fs = [self.submit(fn, *args) for args in zip(*iterables)]
 
@@ -585,7 +585,7 @@ class Executor(object):
                     if timeout is None:
                         yield fs.pop().result()
                     else:
-                        yield fs.pop().result(end_time - time.time())
+                        yield fs.pop().result(end_time - time.monotonic())
             finally:
                 for future in fs:
                     future.cancel()
diff --git a/Misc/NEWS.d/next/Library/2018-09-27-09-45-00.bpo-34819.9ZaFyO.rst b/Misc/NEWS.d/next/Library/2018-09-27-09-45-00.bpo-34819.9ZaFyO.rst
new file mode 100644 (file)
index 0000000..6bbdea2
--- /dev/null
@@ -0,0 +1 @@
+Use a monotonic clock to compute timeouts in :meth:`Executor.map` and :func:`as_completed`, in order to prevent timeouts from deviating when the system clock is adjusted.