English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The sleep() function in Python suspends (waits) the execution of the current thread for the specified number of seconds.
Python has a module namedtimeThe module, which provides some useful functions to handle time-related tasks. One of the commonly used functions is sleep().
The sleep() function pauses the execution of the current thread for the specified number of seconds.
import time print("Immediate print") time.sleep(2.4) print("2.4seconds later print")
The working principle of this program is as follows:
"Immediate print" is output
Pause (delay) execution2.4seconds later.
Print output"2.4seconds later print" .
From the above examples, we can see that sleep() takes a floating-point number as a parameter.
In Python 3.5Before, the actual pause time may be less than the parameter specified for the time() function.
From Python 3.5Start, the pause time will be at least the specified number of seconds.
import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result) time.sleep(1)
In the above program, we calculated and printed an infinitewhile loopthe current local time within1seconds. Similarly, the current local time will be calculated and printed. This process continues.
When you run the program, the output will be similar to:
02:10:50 PM 02:10:51 PM 02:10:52 PM 02:10:53 PM 02:10:54 PM ... .. ...
This is a slightly modified and better version of the above program.
import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result, end="", flush=True) print("\r", end="", flush=True) time.sleep(1)
Before discussing the multi-threaded program of sleep(), let's talk about processes and threads.
A computer program is a set of instructions. A process is the execution of these instructions.
A thread is a subset of a process. A process can have one or more threads.
All the programs above are single-threaded programs. This is an example of a multithreading Python program.
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()
When you run the program, the output will be similar to:
Hello Hello Hi Hello Hi Hi
The above program has two threadst1andt2. These threads use t1.start() and t2.start() statement is executed.
Please note thatt1andt2Running at the same time, you may get different outputs.
The sleep() function pauses the execution of the current thread for the specified number of seconds.
If it is a single-threaded program, sleep() will stop the execution of the thread and process. However, this function suspends the thread rather than the entire process 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 above program has two threads. We have already used these two threads time.sleep(0.5) and time.sleep(0.75) the time it pauses the execution is 0.5seconds and 0.7seconds.