can you tell me how can I get a high precision sleep-function in Python2.6 on Win32 and on Linux?
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.
Use time. This will sleep for half of a second.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With