Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: high precision time.sleep

Tags:

python

can you tell me how can I get a high precision sleep-function in Python2.6 on Win32 and on Linux?

like image 459
George Avatar asked Jul 31 '09 10:07

George


People also ask

Is python time sleep accurate?

The accuracy of the time. sleep function depends on your underlying OS's sleep accuracy. For non-realtime OS's like a stock Windows the smallest interval you can sleep for is about 10-13ms. I have seen accurate sleeps within several milliseconds of that time when above the minimum 10-13ms.

How do you wait 0.5 seconds in python?

Use time. This will sleep for half of a second.

How do you sleep 1 second in python?

If you've got a Python program and you want to make it wait, you can use a simple function like this one: time. sleep(x) where x is the number of seconds that you want your program to wait.

Does time sleep accept floats?

The sleep() function suspends the execution of a program for a given number of seconds. It also takes in floating point numbers as seconds to stop the execution of a program. sleep() is defined in the time module.


2 Answers

You can use floating-point numbers in sleep():

The argument may be a floating point number to indicate a more precise sleep time.

So

time.sleep(0.5) 

will sleep for half a second.

In practice, however, it's unlikely that you will get much more than millisecond precision with sleep because operating systems usually only support millisecond sleeps and because very short amounts of time quickly get unreliable.

like image 63
Joey Avatar answered Sep 20 '22 17:09

Joey


Here is a similar question:

how-accurate-is-pythons-time-sleep

On linux if you need high accuracy you might want to look into using ctypes to call nanosleep() or clock_nanosleep(). I'm not sure of all the implications of trying that though.

like image 21
Karl Voigtland Avatar answered Sep 19 '22 17:09

Karl Voigtland