Python how to delay with a second, a minute, an hour

If you want to delay your code(or delay a function execution) in Python with a second, a minute or an hour you can use module time and sleep. Below you can find the examples of delaying with python 3.7 and time:

  • 1 second:
from time import sleep
sleep(1) # Time is in seconds
  • 5 seconds:
from time import sleep
  • one minute:
from time import sleep
  • one hour:
from time import sleep
sleep(60 * 60)
  • one day:
from time import sleep
sleep(60 * 60 * 24)

If you want to delay a specific function execution then you can check this example:

def weekend():
   print('It weekend!')

sleep(10); weekend()

result(after 10 seconds):

It weekend!

Note: You need to be careful if you work with multithreading or multiprocessing python programs.

Share Tweet Send