Table of Contents
This updated guide examines Multithreading: Function Sleep ()() in Python and organizes the essential facts, background, and practical takeaways in clear American English.
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:
- Execute the task to display the results "Print results to the screen immediately."
- Delay execution within 3 seconds.
- 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, Tipsmake 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.
- Process is the operation of a program.
- 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
FAQ
What is Multithreading: Function Sleep ()() in Python about?
It provides a structured overview of Multithreading, explains the main context, and highlights practical takeaways for readers.
Why does this topic matter?
Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.
How should readers use this information?
Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.
Reader Comments 0
Sign in with email or Google to join the discussion.