PEP 418: time.process_time() falls back on other clock instead of falling

This commit is contained in:
Victor Stinner 2012-04-13 01:21:00 +02:00
parent e0f6584338
commit 03cd4d6a64
1 changed files with 29 additions and 14 deletions

View File

@ -205,27 +205,42 @@ time. It does not include time elapsed during sleep. The reference
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')):
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]
def process_time():
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