]> granicus.if.org Git - python/commitdiff
Close #14690: Use monotonic clock instead of system clock in the sched,
authorVictor Stinner <victor.stinner@gmail.com>
Wed, 30 May 2012 11:30:32 +0000 (13:30 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Wed, 30 May 2012 11:30:32 +0000 (13:30 +0200)
subprocess and trace modules.

Lib/sched.py
Lib/subprocess.py
Lib/trace.py
Misc/NEWS

index a89a11878863ff59bc2f9c2dd31018466ca4a1d5..5551f71df6c0241cb8c5e5754136ce8c0b1c749c 100644 (file)
@@ -35,6 +35,10 @@ try:
     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"]
 
@@ -48,7 +52,7 @@ class Event(namedtuple('Event', 'time, priority, action, argument, kwargs')):
 
 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 = []
index 553f1604059cd1c180421e6e5268ea75479ca6ae..7885ba37afc49e3e682041949cf4084a8b747dc6 100644 (file)
@@ -349,6 +349,10 @@ import signal
 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
@@ -894,7 +898,7 @@ class Popen(object):
             self.wait()
         else:
             if timeout is not None:
-                endtime = time.time() + timeout
+                endtime = _time() + timeout
             else:
                 endtime = None
 
@@ -917,14 +921,14 @@ class Popen(object):
         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)
 
 
@@ -1471,7 +1475,7 @@ class Popen(object):
             # 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)
 
index c0ea090645ab7fd7e591a13c4d6669dcac0d2fa6..41c2f5f69ae87d74764bc4b55a8f2b073eb257a2 100644 (file)
@@ -61,6 +61,10 @@ import gc
 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
@@ -476,7 +480,7 @@ class Trace:
         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:
@@ -614,7 +618,7 @@ class Trace:
             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='')
@@ -627,7 +631,7 @@ class Trace:
             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='')
index 6341af513f50e36b15cf90293f07976e5088a277..ada9acec4dcb122880ab7eadf7efa6fa6b888d59 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -60,6 +60,9 @@ Core and Builtins
 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.