classification
Title: Add time.thread_time()
Type: enhancement Stage: resolved
Components: FreeBSD Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, koobs, pitrou, vstinner
Priority: normal Keywords: patch

Created on 2017-11-14 13:50 by pitrou, last changed 2017-11-20 17:21 by vstinner. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4410 merged pitrou, 2017-11-15 19:02
Messages (9)
msg306210 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-14 13:50
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.
msg306247 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-11-15 01:48
> On other platforms, it can simply raise NotImplementedError.

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().
msg306249 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-11-15 01:54
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.
msg306265 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-15 12:13
Le 15/11/2017 à 02:48, STINNER Victor a écrit :
> 
> Would it be possible to not provide the function if it's not supported by a platform?

Probably, yes.

> Do you know platforms which don't support time.thread_time()?

Any POSIX platform which doesn't have CLOCK_THREAD_CPUTIME_ID?  It's an
optional feature in the POSIX spec.

> Do you want to add time.thread_time_ns() with nanosecond resolution (PEP 564) ?

Yes, I think so.

> 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.

I don't have a macOS machine to test on, though :-)
msg306308 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-11-15 21:06
I can work on the macOS implementation, once your PR will be merged.
msg306309 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-15 21:09
Le 15/11/2017 à 22:06, STINNER Victor a écrit :
> 
> I can work on the macOS implementation, once your PR will be merged.

Great!
msg306313 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-15 21:52
New changeset 4bd41c9b52ea0c730e9e294caaf003e54c088c6e by Antoine Pitrou in branch 'master':
bpo-32025: Add time.thread_time() (#4410)
https://github.com/python/cpython/commit/4bd41c9b52ea0c730e9e294caaf003e54c088c6e
msg306398 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-16 19:39
Should I leave this open for you to work on the macOS implementation, Victor?
msg306570 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-11-20 17:21
I opened bpo-32093 for macOS. I close this one.

Nice enhancement, I already like this new clock ;-)
History
Date User Action Args
2017-11-20 17:21:29vstinnersetstatus: open -> closed

components: + FreeBSD, - Library (Lib)

nosy: + koobs
messages: + msg306570
resolution: fixed
stage: patch review -> resolved
2017-11-16 19:39:54pitrousetmessages: + msg306398
2017-11-15 21:52:25pitrousetmessages: + msg306313
2017-11-15 21:09:32pitrousetmessages: + msg306309
2017-11-15 21:06:33vstinnersetmessages: + msg306308
2017-11-15 19:02:02pitrousetkeywords: + patch
stage: patch review
pull_requests: + pull_request4359
2017-11-15 12:13:43pitrousetmessages: + msg306265
2017-11-15 01:54:55vstinnersetmessages: + msg306249
2017-11-15 01:48:49vstinnersetmessages: + msg306247
2017-11-14 13:50:20pitroucreate