How to limit the speed when creating threads in Python?
You can use the time.sleep() function to control the speed of the thread. This function can pause the execution of the thread for a specified amount of time.
For example, the following code creates a simple thread that prints the thread name every 1 second.
import threading
import time
def print_thread_name():
while True:
print(threading.currentThread().getName())
time.sleep(1)
# 创建线程
thread = threading.Thread(target=print_thread_name)
# 启动线程
thread.start()
In this example, the time.sleep(1) function pauses the thread for 1 second after each loop. By adjusting the parameter of the time.sleep() function, the speed of the thread can be controlled.