Time. Sleep() in Python
Python time sleep () function suspends execution for a certain seconds given by user.
Time sleep () syntax:
Sleep(seconds)
Limitations:
- The number of seconds to be suspends the code as per its requirements.
Returns:
Void
The execution of the program must frequently be stopped to allow for a number of more executions or just for the sake of utility. In such a case, the useful function sleep() can be used to accurately and flexibly suspend the execution of code for any length of time. This function talks about its insight.
Time is a built-in function of the Python time module. Sleep() allows you to postpone a program's execution.
You can be more inventive with the sleep() function in your Python projects because it enables you to build delays that could be quite helpful in enabling particular functionality.
You will discover how to implement delays using the time.sleep() method in this post.
Just keep in mind that delays develop over time.
Sleep() just delays the current thread; it does not halt the entire program's execution.
Example 1:
Generating a time delay in seconds
With a 5 second delay, the start time and end time will be printed.
// python program
importtime
//beginning and printing the start time
Print("begin the execution of time code: ", time.ctime())
// using sleep() to the code execution
time.sleep(5)
// printing the end time
Print("end the execution of time code: ", time.ctime())
Output:
begin the execution of time code: tues mar 9 01:01:01 2020
end the execution of time code:tues mar 9 01:01:01 2020
Example 2:
Generating a time delay in minutes
We are delaying the time for 5 minutes
// python program
importtime
//generating a list
Branches =[‘CSE’, ‘EEE’, ‘ECE’, ‘CIVIL’, ‘MECH’];
//generating a time delay of 5 minutes
time.sleep(5*60)
print(Languages)
Output:
After the delay of 5 minutes, it shown as:
[‘CSE’, ‘EEE’, ‘ECE’, ‘CIVIL’, ‘MECH’]
Applications:
Sleep() is used in a wide variety of contexts. Sleep can be used to implement the background thread execution that occurs repeatedly at pre - determined intervals (). For a great user experience, a common application prints the words letter by letter using the sleep() function.
Example 1:
Generating time delay in the program loop.
// python program
importtime
//declaring string
strn ="JavaTpoint"
//printing javatpoint after each character has been delayed
fori inrange(0, len(strn)):
print(strn[i], end="")
time.sleep(2)
Output:
JavaTpoint
Example 2:
Generating time delay in python list
// python program
importtime
//declaring a time delay of 10 seconds
time.sleep(10)
// creating a list
myList =[‘happy’, ‘new’, ‘year’, 2023]
// the list will be displayed after the delay of 10 seconds
print(myList)
Output:
[‘happy’, ‘new’, ‘year’, 2023]
Example 3:
Generating time delay in python tuple
// python program
importtime
// generating a time delay of 6 seconds
time.sleep(6)
// creating a tuple
mytuple =(‘ramesh’, ‘suresh’, ‘naresh’, ‘jhon’, ‘siri’, ’alexa’)
// the tuple will be showed
print(mytuple)
Output:
[‘ramesh’, ‘suresh’, ‘naresh’, ‘jhon’, ‘siri’, ‘alexa’]
Example 4: list comprehension
// python program
importtime
// generating a time delay of 6 seconds
time.sleep(6)
// creating a tuple
mytuple =(‘ramesh’, ‘suresh’, ‘naresh’, ‘jhon’, ‘siri’, ’alexa’)
// after every 6 seconds item of list gets showed
mytuple =[(time.sleep(6), print(my)) formy inmytuple]
Output:
Ramesh
Suresh
Naresh
John
Siri
Alexa
Example 5: Generating multiple time delays
// python program
importtime
// creating a list
Lang =['Java', 'C++', 'Python', 'Javascript']
// generating a time delay of 7 seconds
time.sleep(7)
// the list will be showed
print(Lang)
forlan inLanguages:
// generating a time delay of 10 seconds
time.sleep(10)
//every 10 seconds an element is showed
print(lang)
Output:
After the delay of 7 seconds, the list will be showed
['Java', 'C++', 'Python', 'Javascript']
Then after every 10 seconds, the element on the list will be showed
Java
C++
Python
Javascript
Python thread sleep
The sleep() method in Python is a crucial multithreading technique. The straightforward example that follows demonstrates how the Python time sleep function simply halts the execution of the currently running thread in multithreaded programming.
Example:
// python program
import time
from threading import Thread
class W(Thread):
def run(self):
for i in range(0, 11):
print(i)
time.sleep(1)
class Wa(Thread):
def run(self):
for j in range(100, 103):
print(j)
time.sleep(5)
print("w started")
W().start()
print("wa started")
Wa().start()
print("finished")
Output:
w started
0
Wa started
100
Finished
1
2
3
4
101
5
6
7
8
9
102
10