Sleeping means suspension of the current thread execution. If a sleep statement is put between two statements, the later one will not get executed until the specified time specified in the sleep statement is elapsed. The sleep statement simply puts the thread in suspension and wait for a signal. After receiving the signal that the specified time is elapsed, it resumes its normal operation.
The Python time module has a function called Sleep() for this purpose. The Sleep() function takes an input in seconds. But the input could be in fraction – a floating point number. So to seep for some milliseconds, we have divide that number by 1000 before passing to the function. For example, if we want to sleep for 50 milliseconds, we have to specify 0.05 (50 / 1000) seconds.
Python Program to Sleep in Milliseconds
import time
print ("Waiting for 50 milliseconds…")
time.sleep (50 / 1000.0) # sleep (0.05)
print ("50 milliseconds elapsed.")
print ("Waiting for 750 milliseconds…")
time.sleep (0.75)
print ("Another 750 milliseconds elapsed.")
Output:
$ python test.py
Waiting for 50 milliseconds…
50 milliseconds elapsed.
Waiting for 750 milliseconds…
Another 750 milliseconds elapsed.