Wait 5 Seconds in Python

Wait 5 Seconds in Python

  1. Use the time.sleep() Function to Wait 5 Seconds in Python
  2. Use the threading Method to Wait 5 Seconds in Python
  3. Use the asyncio.sleep() Function to Wait 5 Seconds in Python
  4. Conclusion

Python has various features and libraries to create interactive applications where users can provide inputs and responses. We can create situations where we need to pause the execution of the application.

This tutorial will discuss how to wait for 5 seconds in Python.

Use the time.sleep() Function to Wait 5 Seconds in Python

Python’s time module provides functionalities and objects to store and manipulate time. The sleep() function from this library adds delays in execution; this function accepts time in seconds.

For example,

import time
print("Before Delay")
time.sleep(5)
print("After delay")

Output:

Before Delay
After delay

In the above example, we paused the program execution and created a delay for 5 seconds using the sleep() function.

Use the threading Method to Wait 5 Seconds in Python

Multi-threading is a technique where we divide the execution of a program into smaller threads that help in faster execution.

Python supports multi-threading and provides the threading library to work with this technique. Two methods from this library can pause execution and wait 5 seconds in Python.

The first method involves using the threading.Event class. Objects of this class allow the threads to communicate to help concurrency execution.

We can use this class’s wait() method to pause execution and wait for 5 seconds in Python.

For example,

import threading
event = threading.Event()
print("Before Delay")
event.wait(5)
print("After delay")

Output:

Before Delay
After delay

Another option is to use the Timer() function. This function can pause the execution for some specified period and then start the thread’s execution, creating a timer for thread execution.

This can be understood better with an example. See the code below.

from threading import Timer

def fun():
    print("After Delay")

print("Before Delay")
t = Timer(5, fun)
t.start()

Output:

Before Delay
After delay

We created a function fun in the above example. This function is called by the Timer() method after waiting for 5 seconds.

Use the asyncio.sleep() Function to Wait 5 Seconds in Python

The asyncio library also helps with threads execution to achieve concurrency. We can create asyncio functions that can wait for a given time before execution.

For this, the sleep() method can be used. It allows the function to pause execution for some time.

We can create the function using the asyncio keyword and execute it using the await keyword. See the code below.

import asyncio

async def main():
    print('Before Delay')
    await asyncio.sleep(5)
    print('After Delay')

await main()

Output:

Before Delay
After delay

The above example works for Python 3.7 and below. This is because the execution of such functions was altered in further versions.

Conclusion

To conclude, we discussed several methods to wait 5 seconds in Python and pause the execution. We used three libraries, time, asyncio, and threading.

The time library provides the most straightforward method with the sleep() function that allows us to pause the program execution for a given time in seconds.

We also demonstrated how to use the threading library for this. We used the Event class and its wait() function to achieve this.

Another method used from this library was the Timer() function, which creates a timer to execute a function.

The asyncio library also provides a sleep() method that can be used to wait for 5 seconds in Python.

Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Python Time

  • The time Module in Python
  • clock() and time() Methods of the Time Module in Python
  • Sleep Milliseconds in Python
  • Calculate Time Elapsed in Python