Whereas working with multithreaded functions in Python, there are conditions the place there’s a have to pause or resume a thread for some cause. As an illustration, to attend for some exterior occasion to happen or to manage the execution order of various threads. In such a state of affairs, the “time.sleep()” technique is utilized in Python to droop the thread execution primarily based on the required time.
This write-up gives an in depth, complete information on the Python thread “time.sleep()” technique by protecting the below-given contents:
What’s the “time.sleep()” Technique in Python?
In Python, the “time.sleep()” technique suspends the processing of the present thread for a specific time period (Seconds). Which means that this system won’t proceed to the subsequent line of code till the required time has handed.
Syntax
Parameter
Within the above code, the “seconds” parameter specifies the variety of seconds to cease the code for. This parameter could be a float, which permits for extra exact timing.
Return Worth
The “time.sleep()” technique retrieves no worth.
Instance 1: Making use of the “time.sleep()” Technique in a Single Thread Program
The given code is utilized to print the worth after a particular time:
print(“Good day”)
time.sleep(3.4)
print(“Python Information”)
Within the above code:
- The string “Good day” is printed instantly after importing the “time” module.
- The “time.sleep()” technique takes the required seconds as an argument and suspends the execution.
- It’s such that the latter string “Python Information” is printed after “3.4” seconds.
Output
The instant and after-suspended time values have been printed and proven within the above output.
Instance 2: Making use of the “time.sleep()” Technique to Create a Python Digital Clock
The beneath instance code makes use of the “time.sleep()” technique to create a Python digital clock:
whereas True:
print(time.strftime(“%I:%M:%S %p”, time.localtime()))
time.sleep(1)
Within the above code:
- The “time” module is imported.
- The “time.strftime()” technique takes the “time format” and the “time.localtime()” technique as its arguments, respectively and retrieves the present time.
- The “time.sleep()” technique is used to droop the execution for “1” second.
- It’s such that after each “1” second, the present native time is printed within the “whereas” loop because the program exceeds the infinite time restrict.
Output
The digital clock has been created.
Instance 3: Making use of the “time.sleep()” Technique in a Multithreading Program
The next instance code makes use of the “time.sleep()” technique in a multithreading program:
import threading
def print_welcome():
for i in vary(5):
time.sleep(0.4)
print(“Welcome”)
def print_bye():
for i in vary(5):
time.sleep(0.6)
print(“Bye”)
time1 = threading.Thread(goal=print_welcome)
time2 = threading.Thread(goal=print_bye)
time1.begin()
time2.begin()
In these code traces:
- The “threading” and “time” modules are imported.
- The 2 capabilities named “print_welcome()” and “print_bye()” are outlined. Every operate prints a message 5 occasions with completely different delays between every print.
- The delay is completely different for every operate i.e., “0.4” seconds for the “print_welcome()” operate and “0.6” seconds for the “print_bye()” operate, respectively.
- The 2 threads “time1” and “time2” are created and related to the required capabilities.
- The threads are then began by calling/accessing the “begin()” technique. Because of this, the goal capabilities can be executed in parallel.
Output
Conclusion
The “time.sleep()” technique of the “time” module in Python is used to droop the execution of the current thread for a sure num/variety of seconds. This technique suspends the execution primarily based on the time and re-executes this system. The “time.sleep()” technique may also work with two-thread packages generally known as multithreading. This text delivered an entire Python “thread sleep” information utilizing quite a few examples.