gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
python写一个线程每隔10秒输出一次
- User
python写一个线程每隔10秒输出一次
- Assistant
以下是一个使用Python编写的代码,可以创建一个线程,每隔10秒输出一次:
import threading import time def print_every_10_seconds(): count = 0 while True: count += 1 print(f"Output {count}") time.sleep(10) thread = threading.Thread(target=print_every_10_seconds) thread.start()这个例子中,我们使用
threading模块创建了一个线程,目标函数为print_every_10_seconds。在这个函数中,我们使用一个循环来不断输出计数,并使用time.sleep(10)来使线程暂停10秒。最后,我们通过调用thread.start()来启动线程。运行这个代码后,你会看到每隔10秒控制台会输出一次“Output X”,其中X是递增的计数值。你可以根据需要修改输出的内容和间隔时间。