[Python-checkins] peps: PEP 418: time.process_time() falls back on other clock instead of falling

victor.stinner python-checkins at python.org
Fri Apr 13 02:17:45 CEST 2012


http://hg.python.org/peps/rev/f3202262d285
changeset:   4238:f3202262d285
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Fri Apr 13 01:21:00 2012 +0200
summary:
  PEP 418: time.process_time() falls back on other clock instead of falling

files:
  pep-0418.txt |  43 ++++++++++++++++++++++++++-------------
  1 files changed, 29 insertions(+), 14 deletions(-)


diff --git a/pep-0418.txt b/pep-0418.txt
--- a/pep-0418.txt
+++ b/pep-0418.txt
@@ -205,27 +205,42 @@
 point of the returned value is undefined, so that only the difference
 between the results of consecutive calls is valid.
 
-Pseudo-code::
+Pseudo-code [#pseudo]_::
 
     if os.name == 'nt':
         def process_time():
             handle = win32process.GetCurrentProcess()
             process_times = win32process.GetProcessTimes(handle)
             return (process_times['UserTime'] + process_times['KernelTime']) * 1e-7
-    elif (hasattr(time, 'clock_gettime')
-          and hasattr(time, 'CLOCK_PROCESS_CPUTIME_ID')):
+    else:
         def process_time():
-            return time.clock_gettime(time.CLOCK_PROCESS_CPUTIME_ID)
-    else:
-        try:
-            import resource
-        except ImportError:
-            def process_time():
-                return _time.clock()
-        else:
-            def process_time():
-                usage = resource.getrusage(resource.RUSAGE_SELF)
-                return usage[0] + usage[1]
+            if process_time.use_process_cputime:
+                try:
+                    return time.clock_gettime(time.CLOCK_PROCESS_CPUTIME_ID)
+                except OSError:
+                    process_time.use_process_cputime = False
+            if process_time.use_getrusage:
+                if process_time.getrusage is None:
+                    try:
+                        import resource
+                    except ImportError:
+                        process_time.use_getrusage = False
+                    else:
+                        def getrusage():
+                            usage = resource.getrusage(resource.RUSAGE_SELF)
+                            return usage[0] + usage[1]
+                        process_time.getrusage = getrusage
+                if process_time.use_getrusage:
+                    try:
+                        return process_time.getrusage()
+                    except OSError:
+                        process_time.use_getrusage = False
+            return _time.clock()
+        process_time.use_process_cputime = (
+            hasattr(time, 'clock_gettime')
+            and hasattr(time, 'CLOCK_PROCESS_CPUTIME_ID'))
+        process_time.use_getrusage = True
+        process_time.getrusage = None
 
 
 Existing functions

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list