Function sleep () in Python

Module time in Python provides some very useful functions to handle time-related tasks. One of the most commonly used functions is sleep () .

The sleep () function in Python is used to stop executing the current thread in the number of seconds passed.

Example 1: Use sleep ()

 import time print ("In kết quả ra màn hình ngay lập tức.") time.sleep(3) print ("In kết quả ra màn hình sau 3s.") 

This method does not return any value but only the executable delay, which works like this:

  1. Execute the task to display the results "Print results to the screen immediately."
  2. Delay execution within 3 seconds.
  3. Continue to perform the task and display "Print results to the screen after 3 seconds."

Example 2: Create an electronic clock in Python

 import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result) time.sleep(1) 

In the above program, Quantrimang created and printed local time inside an infinite while loop. After printing the results, the executable will delay within 1 second and continue printing the current time. This process is continuous thanks to a while loop , forming an electronic clock in Python.

 11:58:31 AM 11:58:32 AM 11:58:33 AM 11:58:34 AM 11:58:35 AM 11:58:36 AM 11:58:37 AM 11:58:38 AM . . . 

Or another way of making electronic clocks:

 import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result, end="", flush=True) print("n", end="", flush=True) time.sleep(1) 

Multithreading in Python

Before talking about sleep () in multi-threaded programs, let's mention a little bit about Process and Thread.

  1. Process is the operation of a program.
  2. A thread is an operating step inside a process. A process can contain multiple threads within it.

Example 3: Python multithreading

 import threading def print_hello_three_times(): for i in range(3): print("Hello") def print_hi_three_times(): for i in range(3): print("Hi") t1 = threading.Thread(target=print_hello_three_times) t2 = threading.Thread(target=print_hi_three_times) t1.start() t2.start() 

Running the program, the output to the screen will look like this:

 Hello Hello Hi Hello Hi Hi 

The above program has two threads t1 and t2. These threads are run using the statements t1.start () and t2.start ().

Note that, t1 and t2 run simultaneously and you can get different output.

time.sleep () in multithreaded programs

The sleep () function pauses execution of the current thread for a certain number of seconds.

In the case of single threaded programs, sleep () pauses thread execution and processing. However, in multithreaded programs, this function only pauses a thread instead of the entire multithreaded process.

Example 4: sleep () in a multithreaded program

 import threading import time def print_hello(): for i in range(4): time.sleep(0.5) print("Hello") def print_hi(): for i in range(4): time.sleep(0.7) print("Hi") t1 = threading.Thread(target=print_hello) t2 = threading.Thread(target=print_hi) t1.start() t2.start() 

The result has the form:

 Hello Hi Hello Hi Hello Hello Hi Hi 

The above program has two threads. Here we used time.sleep (0.5) and time.sleep (0.75) to pause the execution of these two threads in 0.5 seconds and 0.7 seconds respectively.

Last lesson: Module time in Python

5 ★ | 1 Vote

May be interested

  • Generator in PythonPhoto of Generator in Python
    how is the generator different from iterator and what is the usual function, why should we use it? let's find out all through this article.
  • Module time in PythonPhoto of Module time in Python
    python has a time module used to handle time-related tasks. tipsmake.com will work with you to find out the details and functions related to the time specified in this module. let's follow it!
  • Current date and time in PythonPhoto of Current date and time in Python
    how to get the current date and time in python? there are many different ways to do this, and in this article we will apply the class date, class time in the datetime module to display the current date in python.
  • Convert the timestamp value in PythonPhoto of Convert the timestamp value in Python
    instructions to learn how to convert timestamp values ​​into datetime objects and datetime objects into timestamp values ​​in python
  • Strptime () function in PythonPhoto of Strptime () function in Python
    this article shows you how to create a datetime object (date, time, and time) from the corresponding string.
  • Strftime () function in PythonPhoto of Strftime () function in Python
    this article shows you how to convert date, time, and time objects into strings corresponding to a given format.