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:
- 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, 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.
- 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
You should read it
- Module time in Python
- Datetime in Python
- Package in Python
- Current date and time in Python
- Module in Python
- Strptime () function in Python
- For in Python loop
- More than 100 Python exercises have solutions (sample code)
- Strftime () function in Python
- Bookmark 5 best Python programming learning websites
- Python data type: string, number, list, tuple, set and dictionary
- Manage files and folders in Python
Maybe you are interested
How to share YouTube Music over time
It's time to switch to Passkey: The anti-phishing password alternative!
How to talk to a girl you like for the first time
6 signs it's time to upgrade your Smart TV
5 most powerful stealth weapons of all time of America
[99% Fixed] Clock Watchdog Timeout Blue Screen Error