Skip to content

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

@fusselkater

Description

@fusselkater

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions