You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, the time module has time.process_time(), a cross-platform function for getting per-process elapsed CPU time.
Similarly, we could expose time.thread_time(), to get per-thread elapsed CPU time.
On a modern POSIX platform, it can use clock_gettime(CLOCK_THREAD_CPUTIME_ID).
On Windows, it can use GetThreadTimes(): https://msdn.microsoft.com/en-us/library/ms683237%28VS.85%29.aspx
On other platforms, it can simply raise NotImplementedError.
Currently, you would need ctypes hacks to call GetThreadTimes(), which is not very nice.
Activity
pitrou commentedon Nov 14, 2017
Currently, the time module has time.process_time(), a cross-platform function for getting per-process elapsed CPU time.
Similarly, we could expose time.thread_time(), to get per-thread elapsed CPU time.
On a modern POSIX platform, it can use clock_gettime(CLOCK_THREAD_CPUTIME_ID).
On Windows, it can use GetThreadTimes(): https://msdn.microsoft.com/en-us/library/ms683237%28VS.85%29.aspx
On other platforms, it can simply raise NotImplementedError.
Currently, you would need ctypes hacks to call GetThreadTimes(), which is not very nice.
vstinner commentedon Nov 15, 2017
Would it be possible to not provide the function if it's not supported by a platform?
Do you know platforms which don't support time.thread_time()?
Do you want to add time.thread_time_ns() with nanosecond resolution (PEP-564) ? I suggest to add it to be consistent with time.process_time().
vstinner commentedon Nov 15, 2017
It seems like macOS doesn't implement CLOCK_THREAD_CPUTIME_ID but provides thread_info() which can be used by suming user and system times.
pitrou commentedon Nov 15, 2017
Le 15/11/2017 à 02:48, STINNER Victor a écrit :
Probably, yes.
Any POSIX platform which doesn't have CLOCK_THREAD_CPUTIME_ID? It's an
optional feature in the POSIX spec.
Yes, I think so.
I don't have a macOS machine to test on, though :-)
vstinner commentedon Nov 15, 2017
I can work on the macOS implementation, once your PR will be merged.
pitrou commentedon Nov 15, 2017
Le 15/11/2017 à 22:06, STINNER Victor a écrit :
Great!
pitrou commentedon Nov 15, 2017
New changeset 4bd41c9 by Antoine Pitrou in branch 'master':
bpo-32025: Add time.thread_time() (bpo-4410)
4bd41c9
pitrou commentedon Nov 16, 2017
Should I leave this open for you to work on the macOS implementation, Victor?
vstinner commentedon Nov 20, 2017
I opened bpo-32093 for macOS. I close this one.
Nice enhancement, I already like this new clock ;-)