Is Sleep() Accurate Enough In Python?
I have created a python script that records the up-time of PC throughout the day, and stores the usage in a database the next day. while 1: time.sleep(59.9) # now s
Solution 1:
You can try GetTickCount64 function.
Exmaple:
import ctypes
import datetime
defuptime_in_ms():
lib = ctypes.windll.kernel32
return lib.GetTickCount64()
deffrmt(dt):
d = dt.days
_s = dt.seconds
h, rem = divmod(_s, 3600)
m, s = divmod(rem, 60)
returnf'{d} day(s), {h:02}h:{m:02}m:{s:02}s'if __name__ == '__main__':
ms = uptime_in_ms()
dt = datetime.timedelta(milliseconds=ms)
uptime = frmt(dt)
print(uptime)
Testing:
C:\Users\User>python Desktop/get_uptime.py
1day(s), 00h:18m:51s
Notes:
- Minimum supported client - Windows Vista
- This is just an example so there is no validation if this library exists
- This example is using f-string formatting - Python 3.6+
Post a Comment for "Is Sleep() Accurate Enough In Python?"