Python time sleep () function suspends execution for a certain seconds given by user.
Time sleep () syntax:
- The number of seconds to be suspends the code as per its requirements.
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.
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())
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
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)
After the delay of 5 minutes, it shown as: [‘CSE’, ‘EEE’, ‘ECE’, ‘CIVIL’, ‘MECH’]
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.
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)
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)
[‘happy’, ‘new’, ‘year’, 2023]
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)
[‘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]
Ramesh Suresh Naresh John Siri Alexa
Example 5: Generating multiple time delays
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.
// 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")
w started 0 Wa started 100 Finished 1 2 3 4 101 5 6 7 8 9 102 10