python sleep 5 examples

If you want to delay your program with certain amount of time or until something happen you can use module time function sleep:

time.sleep(secs)

example:

import time

for i in range(10):
    print("Every second." )
    time.sleep(1)   # Delay for (10 seconds in total (1 seconds per iteration).

while True:
    print("Every 10 seconds.")
    time.sleep(10)   # Delay for inifinity (10 seconds).

time.sleep(50 / 1000)           # 50 ms
time.sleep(5)                   # 5 secs
time.sleep(60)                  # 1 min
time.sleep(60 * 60)             # 1 hour
time.sleep(60 * 60 * 24)        # 1 day

Have in mind that accuracy of this function is about 10 20 ms depending on the plarform. So in the example:

time.sleep(50 / 1000)           # 50 ms

the tread will be slept for 50 ms +- 10 secs.

Related Article