Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add time.thread_time() #76206

Closed
pitrou opened this issue Nov 14, 2017 · 9 comments
Closed

Add time.thread_time() #76206

pitrou opened this issue Nov 14, 2017 · 9 comments
Labels
3.7 type-feature

Comments

@pitrou
Copy link
Member

@pitrou pitrou commented Nov 14, 2017

BPO 32025
Nosy @abalkin, @pitrou, @vstinner, @koobs
PRs
  • #4410
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-11-20.17:21:29.014>
    created_at = <Date 2017-11-14.13:50:20.131>
    labels = ['type-feature', '3.7']
    title = 'Add time.thread_time()'
    updated_at = <Date 2017-11-20.17:21:29.004>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2017-11-20.17:21:29.004>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-11-20.17:21:29.014>
    closer = 'vstinner'
    components = ['FreeBSD']
    creation = <Date 2017-11-14.13:50:20.131>
    creator = 'pitrou'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32025
    keywords = ['patch']
    message_count = 9.0
    messages = ['306210', '306247', '306249', '306265', '306308', '306309', '306313', '306398', '306570']
    nosy_count = 4.0
    nosy_names = ['belopolsky', 'pitrou', 'vstinner', 'koobs']
    pr_nums = ['4410']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue32025'
    versions = ['Python 3.7']

    @pitrou
    Copy link
    Member Author

    @pitrou pitrou commented 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.

    @pitrou pitrou added stdlib 3.7 type-feature labels Nov 14, 2017
    @vstinner
    Copy link
    Member

    @vstinner vstinner commented Nov 15, 2017

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

    @vstinner
    Copy link
    Member

    @vstinner vstinner commented 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
    Copy link
    Member Author

    @pitrou pitrou commented Nov 15, 2017

    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 :-)

    @vstinner
    Copy link
    Member

    @vstinner vstinner commented Nov 15, 2017

    I can work on the macOS implementation, once your PR will be merged.

    @pitrou
    Copy link
    Member Author

    @pitrou pitrou commented Nov 15, 2017

    Le 15/11/2017 à 22:06, STINNER Victor a écrit :

    I can work on the macOS implementation, once your PR will be merged.

    Great!

    @pitrou
    Copy link
    Member Author

    @pitrou pitrou commented Nov 15, 2017

    New changeset 4bd41c9 by Antoine Pitrou in branch 'master':
    bpo-32025: Add time.thread_time() (bpo-4410)
    4bd41c9

    @pitrou
    Copy link
    Member Author

    @pitrou pitrou commented Nov 16, 2017

    Should I leave this open for you to work on the macOS implementation, Victor?

    @vstinner
    Copy link
    Member

    @vstinner vstinner commented Nov 20, 2017

    I opened bpo-32093 for macOS. I close this one.

    Nice enhancement, I already like this new clock ;-)

    @vstinner vstinner removed the stdlib label Nov 20, 2017
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 type-feature
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants