Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-135304: Reported resolution oftime.process_time andtime.thread_time is wrong on Windows#135305

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
chris-eibl wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromchris-eibl:fix_resolution

Conversation

chris-eibl
Copy link
Member

@chris-eiblchris-eibl commentedJun 9, 2025
edited
Loading

>>>importtime>>>time.get_clock_info("process_time")namespace(implementation='GetProcessTimes()',monotonic=True,adjustable=False,resolution=1e-07)>>>time.get_clock_info("thread_time")namespace(implementation='GetThreadTimes()',monotonic=True,adjustable=False,resolution=1e-07)

I suggest to report15.625 ms, since this is the upper limit.

@chris-eibl
Copy link
MemberAuthor

The existing tests

self.assertIsInstance(info.resolution,float)
# 0.0 < resolution <= 1.0
self.assertGreater(info.resolution,0.0)
self.assertLessEqual(info.resolution,1.0)

still pass and are IMHO sufficient: no need to hardcode platform specific values here.

I've manually verified the new values:

>>>time.get_clock_info("process_time")namespace(implementation='GetProcessTimes()',monotonic=True,adjustable=False,resolution=0.015625)>>>time.get_clock_info("thread_time")namespace(implementation='GetThreadTimes()',monotonic=True,adjustable=False,resolution=0.015625)

@chris-eibl
Copy link
MemberAuthor

cc@vstinner since#82040 (comment)

Patches are welcome to enhance time.get_clock_info() :-)

@chris-eiblchris-eibl requested a review fromvstinnerJune 9, 2025 15:44
@chris-eiblchris-eibl changed the titleGH-135304: Reported resolution of time.process_time and time.thread_time is wrong on WindowsGH-135304: Reported resolution oftime.process_time andtime.thread_time is wrong on WindowsJun 9, 2025
@zooba
Copy link
Member

This isn't the way, I'm afraid. The specified resolution is all we know, and undocumented behaviours like the resolution a system is actually going to write into the data structures isn't something we should be hard coding and relying on.

These functions should really only be used for reporting, not for decision making. If tests want to use them for decision making, then they also need a control to verify that they're going to work as intended (e.g. run a known amount of work and make sure that it registers in these functions).

At worst, we should try and read the current interval, rather than hard-coding the worst case scenario. But even that's risky, as it can change while the system is running (not a great idea, because programsdo rely on it, and they can get into trouble as it changes, but we shouldn't be encouraging people to write code like tht).

chris-eibl reacted with thumbs up emoji

@chris-eibl
Copy link
MemberAuthor

chris-eibl commentedJun 9, 2025
edited
Loading

Ah, ok, I see. Even the PEP (https://peps.python.org/pep-0418/#process-time) lists

NameOperating systemOS ResolutionPython Resolution
GetProcessTimes()Windows Seven16 ms16 ms

These functions should really only be used for reporting, not for decision making

Seems to happen even in the stdlib

# Ensuring that we chose a slow enough conversion to measure.
# It takes 0.1 seconds on a Zen based cloud VM in an opt build.
# Some OSes have a low res 1/64s timer, skip if hard to measure.
ifsw_convert.seconds<sw_convert.clock_info.resolution*2:

That's where I came from :)

Maybe just adocumentation issue?

Converting to draft tagging asDO-NOT-MERGE ...

@zooba
Copy link
Member

The PEP isn't the documentation of the OS. If you can find a reference on learn.microsoft.com that states the resolution of that specific function, then we can lock it in. But you won't, because the resolution of the actual timer is deliberately left unspecified. (You'll find examples showing that it's typically around 15ms and adjustable down for some benefit in some situations, but no guarantees.)

A lot of code in the stdlib, and especially its test suite, is "best effort" to make something work. It's not necessarily a robust example of the best way to implement something. Trust the upstream docs, just like we expect people to trust ours 😉

chris-eibl reacted with thumbs up emoji

@chris-eibl
Copy link
MemberAuthor

Oh yes, I frequently read the MSDN :)

With

Maybe just a documentation issue?

I meant we could add some hints about that tohttps://docs.python.org/3/library/time.html#time.get_clock_info

resolution: The resolution of the clock in seconds (float)

Specifically something like

These functions should really only be used for reporting, not for decision making

@zooba
Copy link
Member

Clarifying documentation is always fine, provided it doesn't specify us into a corner. Something along the lines of "this is a theoretical resolution, and not necessarily the rate at which the values are updated" and "these functions should not be treated as high-precision timers on most operating systems" would be fine.

chris-eibl reacted with thumbs up emoji

@vstinner
Copy link
Member

I suggest to report 15.625 ms, since this is the upper limit.

time.get_clock_info() should report the lower limit. On Linux, it reports 1 nanosecond for most clocks even if it's the actual resolution, just because the C structure has a resolution of 1 nanosecond.

Yes, documentation changes are welcomed to clarify that.

Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The change is not correct.

@bedevere-app
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phraseI have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@chris-eibl
Copy link
MemberAuthor

time.get_clock_info() should report the lower limit.

IIUC,@zooba would oppose (?) because

But you won't, because the resolution of the actual timer is deliberately left unspecified.

So I think we shouldn't change anything but just add some notes to the documentation?

Best to close this PR and keep the the discussion on the issue? And then (maybe) create a documentation PR?

@chris-eiblchris-eibl marked this pull request as draftJune 10, 2025 15:05
@vstinner
Copy link
Member

So I think we shouldn't change anything but just add some notes to the documentation?

Correct. Unless the OS provides an API to query the resolution.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@vstinnervstinnervstinner requested changes

@pgansslepganssleAwaiting requested review from pgansslepganssle is a code owner

@abalkinabalkinAwaiting requested review from abalkinabalkin is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@chris-eibl@zooba@vstinner

[8]ページ先頭

©2009-2025 Movatter.jp