Python 在 n 秒后停止一个线程
Python stop a thread after n seconds
如何在 5 秒后停止线程?我想每秒钟打印 "Hello World" ,然后在 5 秒后停止。这是我的代码:
import threading
def printthis():
threading.Timer(1.0, printthis).start()
print("Hello, World!")
printthis()
这个怎么样:
def printthis():
print_count = 0
while print_count < 5: # 5 because print_count will be 5 after printing the 5th time
threading.Timer(1.0, printthis).start()
print("Hello, World!")
print_count += 1
printthis()
如何在 5 秒后停止线程?我想每秒钟打印 "Hello World" ,然后在 5 秒后停止。这是我的代码:
import threading
def printthis():
threading.Timer(1.0, printthis).start()
print("Hello, World!")
printthis()
这个怎么样:
def printthis():
print_count = 0
while print_count < 5: # 5 because print_count will be 5 after printing the 5th time
threading.Timer(1.0, printthis).start()
print("Hello, World!")
print_count += 1
printthis()