Commits

Victor Stinner committed 1345cf5

Close #14690: Use monotonic clock instead of system clock in the sched,
subprocess and trace modules.

Comments (0)

Files changed (4)

     import threading
 except ImportError:
     import dummy_threading as threading
+try:
+    from time import monotonic as _time
+except ImportError:
+    from time import time as _time
 
 __all__ = ["scheduler"]
 
 
 class scheduler:
 
-    def __init__(self, timefunc=time.time, delayfunc=time.sleep):
+    def __init__(self, timefunc=_time, delayfunc=time.sleep):
         """Initialize a new instance, passing the time and delay
         functions"""
         self._queue = []

Lib/subprocess.py

 import builtins
 import warnings
 import errno
+try:
+    from time import monotonic as _time
+except ImportError:
+    from time import time as _time
 
 # Exception classes used by this module.
 class SubprocessError(Exception): pass
             self.wait()
         else:
             if timeout is not None:
-                endtime = time.time() + timeout
+                endtime = _time() + timeout
             else:
                 endtime = None
 
         if endtime is None:
             return None
         else:
-            return endtime - time.time()
+            return endtime - _time()
 
 
     def _check_timeout(self, endtime, orig_timeout):
         """Convenience for checking if a timeout has expired."""
         if endtime is None:
             return
-        if time.time() > endtime:
+        if _time() > endtime:
             raise TimeoutExpired(self.args, orig_timeout)
 
 
             # printing.
             if endtime is not None or timeout is not None:
                 if endtime is None:
-                    endtime = time.time() + timeout
+                    endtime = _time() + timeout
                 elif timeout is None:
                     timeout = self._remaining_time(endtime)
 
 import dis
 import pickle
 from warnings import warn as _warn
+try:
+    from time import monotonic as _time
+except ImportError:
+    from time import time as _time
 
 try:
     import threading
         self._caller_cache = {}
         self.start_time = None
         if timing:
-            self.start_time = time.time()
+            self.start_time = _time()
         if countcallers:
             self.globaltrace = self.globaltrace_trackcallers
         elif countfuncs:
             self.counts[key] = self.counts.get(key, 0) + 1
 
             if self.start_time:
-                print('%.2f' % (time.time() - self.start_time), end=' ')
+                print('%.2f' % (_time() - self.start_time), end=' ')
             bname = os.path.basename(filename)
             print("%s(%d): %s" % (bname, lineno,
                                   linecache.getline(filename, lineno)), end='')
             lineno = frame.f_lineno
 
             if self.start_time:
-                print('%.2f' % (time.time() - self.start_time), end=' ')
+                print('%.2f' % (_time() - self.start_time), end=' ')
             bname = os.path.basename(filename)
             print("%s(%d): %s" % (bname, lineno,
                                   linecache.getline(filename, lineno)), end='')
 Library
 -------
 
+- Issue #14690: Use monotonic clock instead of system clock in the sched,
+  subprocess and trace modules.
+
 - Issue #14958: Change IDLE systax highlighting to recognize all string and
   byte literals supported in Python 3.3.