import threading
# 创建一个定时器,使用 threading.Timer 类
def timer_example():
# 定义定时任务函数
def hello():
print("Hello, World!")
# 创建一个定时器对象,5秒后执行hello函数
timer = threading.Timer(5.0, hello)
# 启动定时器
timer.start()
print("Timer started.")
# 调用示例函数
timer_example()
threading 模块,它提供了对线程操作的支持。hello() 函数是我们希望在定时器触发时执行的任务。threading.Timer(5.0, hello) 创建一个定时器对象,表示 5 秒后执行 hello 函数。timer.start() 启动定时器。这段代码会在启动定时器后等待 5 秒,然后输出 "Hello, World!"。
下一篇:python hashmap
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站