| by Arround The Web | No comments

Python Thread Sleep

While working with multithreaded applications in Python, there are situations where there is a need to pause or resume a thread for some reason. For instance, to wait for some external event to occur or to control the execution order of different threads. In such a situation, the “time.sleep()” method is used in Python to suspend the thread execution based on the specified time.

This write-up provides a detailed, comprehensive guide on the Python thread “time.sleep()” method by covering the below-given contents:

What is the “time.sleep()” Method in Python?

In Python, the “time.sleep()” method suspends the processing of the current thread for a particular period of time (Seconds). This means that the program will not continue to the next line of code until the specified time has passed.

Syntax

time.sleep(seconds)

Parameter

In the above code, the “seconds” parameter specifies the number of seconds to stop the code for. This parameter can be a float, which allows for more precise timing.

Return Value

The “time.sleep()” method retrieves no value.

Example 1: Applying the “time.sleep()” Method in a Single Thread Program

The given code is utilized to print the value after a specific time:

import time

print("Hello")

time.sleep(3.4)

print("Python Guide")

In the above code:

  • The string “Hello” is printed immediately after importing the “time” module.
  • The “time.sleep()” method takes the specified seconds as an argument and suspends the execution.
  • It is such that the latter string “Python Guide” is printed after “3.4” seconds.

Output

The immediate and after-suspended time values have been printed and shown in the above output.

Example 2: Applying the “time.sleep()” Method to Create a Python Digital Clock

The below example code uses the “time.sleep()” method to create a Python digital clock:

import time

while True:

print(time.strftime("%I:%M:%S %p", time.localtime()))

time.sleep(1)

In the above code:

  • The “time” module is imported.
  • The “time.strftime()” method takes the “time format” and the “time.localtime()” method as its arguments, respectively and retrieves the current time.
  • The “time.sleep()” method is used to suspend the execution for “1” second.
  • It is such that after every “1” second, the current local time is printed in the “while” loop since the program exceeds the infinite time limit.

Output

The digital clock has been created.

Example 3: Applying the “time.sleep()” Method in a Multithreading Program

The following example code uses the “time.sleep()” method in a multithreading program:

import time

import threading

def print_welcome():

for i in range(5):

time.sleep(0.4)

print("Welcome")

def print_bye():

for i in range(5):

time.sleep(0.6)

print("Bye")

time1 = threading.Thread(target=print_welcome)

time2 = threading.Thread(target=print_bye)

time1.start()

time2.start()

In these code lines:

  • The “threading” and “time” modules are imported.
  • The two functions named “print_welcome()” and “print_bye()” are defined. Each function prints a message five times with different delays between each print.
  • The delay is different for each function i.e., “0.4” seconds for the “print_welcome()” function and “0.6” seconds for the “print_bye()” function, respectively.
  • The two threads “time1” and “time2” are created and associated with the specified functions.
  • The threads are then started by calling/accessing the “start()” method. As a result, the target functions will be executed in parallel.

Output

Conclusion

The “time.sleep()” method of the “time” module in Python is used to suspend the execution of the present thread for a certain num/number of seconds. This method suspends the execution based on the time and re-executes the program. The “time.sleep()” method can also work with two-thread programs known as multithreading. This article delivered a complete Python “thread sleep” guide using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply