4

How can I accurately pause for a set length of time in python.

I've tried:

import time
time.sleep(amount)

(amount = a length in time less than 1 second)

however this isn't very accurate, is there any thing better?

(windows 10 , 64 bit)

jack
  • 90
  • 1
  • 8
  • 11
    Please indicate what sort of precision you need. – Reblochon Masque Sep 03 '18 at 06:31
  • How accurate? I think this depends on OS, `vxworks` may meet your requirements if you are so care about accurate not os for consumer. – atline Sep 03 '18 at 06:32
  • I'm simply wondering if there is more accurate way of doing this. – jack Sep 03 '18 at 06:33
  • @jack - were might be, but it depends on your operating system. Which OS are you using? – cdarke Sep 03 '18 at 06:34
  • Possible duplicate of [Correct way to pause Python program](https://stackoverflow.com/questions/11552320/correct-way-to-pause-python-program) – Ankur Sinha Sep 03 '18 at 06:38
  • Use [`Timer`](https://docs.python.org/3/library/threading.html?highlight=timer#timer-objects) maybe? – KaiserKatze Sep 03 '18 at 06:39
  • Maybe a glance for a very old post: https://stackoverflow.com/questions/1133857/how-accurate-is-pythons-time-sleep ? – atline Sep 03 '18 at 06:40
  • Possible duplicate of [How accurate is python's time.sleep()?](https://stackoverflow.com/questions/1133857/how-accurate-is-pythons-time-sleep) – OneCricketeer Sep 03 '18 at 06:50

2 Answers2

3

sleep will never be more accurate than the operating system's tick rate, for windows it's somewhere in the 10ms-16ms range , and in linux it can be as low as 1ms depending on the OS but typically it will be higher like windows

if you want a higher precision sleep and if you're on linux you can consider using nanosleep() which is a high precision sleep function that unfortunately isn't exposed by python in any built in library - so you will need to create a small .so file which exposes it to python (either by importing it as is with ctypes or by making a C python module with cpython) this function according to the docs will be as precise as the timer itself can be

there are also high precision solutions for windows - but i never used any of them so i'm not qualified to recommend what to use on windows (but again you will need a .dll that exposes such methods to python)

with all that said if you dont care about power usage you can just do a busy sleep:

import time
def busy_sleep(seconds_to_sleep):
    start = time.time()
    while (time.time() < start + seconds_to_sleep):
        pass

then you can call it like this busy_sleep(0.005) and it will "sleep" almost exactly 0.005 seconds "sleep" is in air quotes since you aren't really sleeping you're keeping the cpu busy all this time at 100% but if what's important for you is to delay execution in a precise manner it could still work for you

AntiMatterDynamite
  • 1,495
  • 7
  • 17
2

Have you tried this? If I used time.sleep(1) it would wait one second. So,

time.sleep(0.33333333333) 
# a third of a second pause.#
time.sleep(0.5)
# a half of a second pause.#

and so on. I'm sorry if this didn't awnser your question in the way you wanted!