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

Using time.sleep(1) is not accurate #1

Open
fusselkater opened this issue Nov 2, 2023 · 1 comment
Open

Using time.sleep(1) is not accurate #1

fusselkater opened this issue Nov 2, 2023 · 1 comment

Comments

@fusselkater
Copy link

As all modern OS implement some kind of multi tasking, when you call sleep, your process will be set to sleeping state until the sleep time has elapsed. But it will not reactivate your process exactly after 1 second, instead it will be 1 second + some more or less random x.
So this timer is not accurate especially for long durations.
The better way to implement this would be to save your current system time when you click start (e.g. with starttime = datetime.datetime.now()). Then while running, substract current time from starttime:

import datetime
starttime = datetime.datetime.now()

# later
delta = datetime.datetime.now() - starttime

delta will be a datetime.timedelta object with minutes/seconds/milliseconds that elapsed since starttime.

@Sonic2kDBS
Copy link
Owner

Sonic2kDBS commented Nov 15, 2023

Hi, thank you for bringing that up. You are right. I didn't thought about that until you mentioned it.
I did some testing and there is indeed a time difference. The difference is about 1/2 Second on 10 minutes, so 3 seconds on an hour. For the full slider time of 120 minutes it is 6 seconds.

Thank you for your suggestions. I will use them in the next release to make the Desk-Timer more precise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants