What is Python Sleep?
Syntax
Example: Using sleep() function in Python
How to delay the execution of function using sleep()?
What are the different ways to add a delay in Python Script?
Using asyncio.sleep function available from (Python 3.4 or higher)
Using Event().wait
Using Timer
time.sleep() Syntax
Parameters:
seconds: The number of seconds you want the execution of your code to be halted.
Example: Using sleep() function in Python
Follow the steps given below to add sleep() in your python script. Step 1: Step 2: Add time.sleep() The number 5 given as input to sleep(), is the number of seconds you want the code execution to halt when it is executed. Here is a working code along with messages inside print(), to show the delay of message display on the terminal when executed. Output:
How to delay the execution of function using sleep()?
The example shown below has a function defined called display(). The display() function prints a message “Welcome to Guru99 Tutorials”. When the function is called, it will execute and display the message inside the terminal. To add delay to the execution of the function, let us add the time.sleep in Python before making a call to the function. During the execution, Python time.sleep will halt there for the number of seconds given, and later the function display() will be called. Example: Output:
What are the different ways to add a delay in Python Script?
Using sleep() function
We have seen a few examples earlier on how to use time.sleep(). Let us try a different example here using time.sleep(). The code has a for loop that will take the string variable and print each character with a delay of 1 seconds. Output:
Using asyncio.sleep function available from (Python 3.4 or higher)
You can make use of asyncio.sleep with python version 3.4 and higher. To make use of the asyncio sleep method, you need to add async and await to the function, as shown in the example below: Example: The script has a function call display() that prints a message “Welcome to Guru99 tutorials”. There are two keywords used in the function async and await. The async keyword is added at the start of the function definition, and await is added just before the asyncio.sleep(). Both the keywords async / await are meant to handle the asynchronous task. When the function display() is called, and it encounters await asyncio.sleep(5), the code will sleep or halt at that point for 5 seconds and, once done, will print the message. Output:
Using Event().wait
The Event().wait method comes from the threading module. Event.wait() method will halt the execution of any process for the number of seconds it takes as an argument. The working of Event is shown in the example below: Example: The code is using Event().wait(5).The number 5 is the number of seconds the code will delay to go to the next line that calls the function display(). Once the 5 seconds are done, the function display() will be called, and the message will be printed inside in the terminal. Output:
Using Timer
The Timer is another method available with Threading, and it helps to get the same functionality as Python time sleep. The working of the Timer is shown in the example below: Example: A Timer takes in input as the delay time in Python in seconds, along with a task that needs to be started. To make a timer working, you need to call the start() method. In the code, the Timer is given 5 seconds, and the function display that has to be called when 5 seconds are done. The timer will start working when the Timer.start() method is called. Output:
Summary:
Python sleep() function will pause Python code or delay the execution of program for the number of seconds given as input to sleep(). The sleep() function is part of the Python time module. You can make use of Python sleep function when you want to temporarily halt the execution of your code. For example, in case you are waiting for another process to complete, or a file upload, etc. There are many ways to add Python delay function to code besides sleep, and they are using asyncio.sleep , Event().wait and Timer. Similar to sleep() method, there is asyncio.sleep() method with python version 3.4 and higher. To make use of the asyncio sleep method, you need to add async and await to the function The Event().wait method comes from the threading module. Event.wait() method will halt the execution of any process for the number of seconds it takes as an argument. The Timer is another method available with Threading, and it helps to get the same functionality as sleep