Python

Python Thread Sleep

We can suspend the execution of the program for a set amount of time by utilizing the “sleep()” method in Python. When we create code, it runs line by line. However, if we want to pause execution for a little while, we can use the “time.sleep()” method. By using the “time.sleep()” method, we can put the thread to sleep. To put it simply, the “sleep()” function stops the current thread from operating for a predetermined amount of time.

Example 1: Utilizing Simple Code to Implement the Sleep() Method in Python.

In this example, we’ll use Python’s “sleep()” function to implement a code. The Python “sleep()” technique is always essential when we wish to suspend the current thread’s operation or for a certain amount of time. Python’s “time” module provides several essential applications to perform time-related operations. The “sleep()” function is among the more well-known ones. Any time a thread is mentioned, the “sleep()” function will halt its operation. The real suspension time can be less than that specified because every captured signal will end the sleep() after its catching function has been executed.

Let’s now begin to execute the code. First, we must import the “time” module. Then, in the next line, we call the “print()” function. The “print()” function outputs the statement to the normal output device, such as the screen. The following statement will be passed to the “print()” function’s parenthesis “This phrase immediately prints: Python is the simplest programming language”.

Now, after printing this statement after importing the time module, we must print it immediately when the code is executed. Or, to put it simply, this statement prints as in regular times as the other programs print their output. Now, the next time we use “time.sleep(),” we pass the seconds as its parameter, which is “5”,. We again call the “print()” function. We are passing the statement we want to appear on the screen after five seconds in the print function’s parentheses. This statement is “This statement prints after five seconds: I love coding”.

When we execute the code, the first statement is displayed right away but the second statement is displayed after “5” seconds since we put the time “5” seconds” in the parentheses of the “sleep()” function. Here, time is the library, and we call this “sleep()” function using the module.

When we pressed the run button, the output showed this statement right away because we didn’t set a timer for it. As a result, it printed immediately, just like other programs do when we print their output.

The second statement is now shown, as seen in the figure. This statement suspends execution for “5” seconds and is displayed after that time because the time parameter for the “sleep()” function was set to “5” seconds.

Example 2: Utilizing the “sleep()” Function in Python to Generate a Gigital Clock.

In this example, we’ll use the “time.sleep()” function to make a digital clock, which we’ll show in the output. First, we import the Python time package as “t” and compute the time inside an endless while loop. In a while loop in Python, a block of code is iterated over while the iteration cycle condition is true. We typically use this loop when we are unclear about how many times to iterate. While a succession of claims is made an undefined number of times while being true. This type of loop starts working when a specific condition is True and doesn’t stop until the condition is False. Then, in the following line, we use the method “localtime()” which converts the number of seconds into local time using the time module “t”. If seconds are not provided or if None, the actual time as returned by time() is used. We added the initialized variable “localtime” with the value “t.localtime()”.

Now in the next line, we call “strftime()” with the “t” and set the time in its parenthesis (%I:%M:%S%P), and pass the variable “localtime” as well. Then, in the following line, we used the “sleep()” function with the module “t” and set the time to “2” seconds in its parentheses.

As a result, the current time will now be printed in the output display. But because we set the time to “2” seconds, the program will first wait for 2 seconds before computing and printing the current time again.

Now that “2” seconds have been set inside the “sleep()” method, the output shows a digital clock with the current time and suspends execution for two seconds before printing the next time. The order of time is “hour/minutes/second/AM” so the current time that the digital clock displayed initially is “08:29:43:AM” and the next time it displayed, it did so after two seconds. So, you can see that the seconds have changed the previous one was “43” and the following one was “45” similarly, it continues.

Example 3: Using Python time.sleep() in Multithreading

The “time.sleep()” function will be used in this example to demonstrate multithreading. The examples above used single threading, whereas this example used two threads. Let’s begin by importing two libraries. The first is called “threading” and the second is called “time”. Then, we define the function “print k” in the following line. In this example, we first create a function, which is a user define function. Then, on the second sequence, we set the range to “for m in range(3)” and then we use “time.sleep(),” and pass the statement “I love coding” inside the parentheses.

Subsequently, we invoke the “print()” function and pass the statement “I love coding” which means that this statement will print three times in the output displays. For “1” second, this thread pauses the execution. Afterward, we create a second function called “print l” and set its range to “3” before calling the “time.sleep()” function and passing the value “1.5” second. After that, we call the “print()” function and pass the statement “I love Python” to complete the same process, which will print this statement three times while taking a moment execution for “1.5” seconds.

Furthermore, we create the variables “t1” and “t2” and use “threading.thread” to call the respective functions. Then, in the following line, we utilize “t1.start()” and “t2.start()”. To start the thread’s activity, the start() method of the thread must be invoked after the thread object has been created. This invokes the execute() method and starts a new control thread. A thread is considered “active” when something happens in it.

The first time the system reads, it takes more time to read the function and displays the statements sparingly. However, the second time, the same statement is written twice since the data is saved in the system’s memory and therefore takes less time to access. You can now see in the output that both statements were printed three times because we set ranges three times.

Conclusion

In this article, we covered both single-threaded and multi-threaded uses of the Python function “time.sleep()”. In the first two cases, we delayed the execution for a predetermined period using the single-threaded “sleep()” function. In the third example, we used two threads to suspend execution for a predetermined period. To choose how frequently we want to publish the function statement, we also define the range of the statements. The purpose of this article is to help you manage tasks utilizing Python thread sleep.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.