vpython有没有定时执行任务的库?
网友回复
是的,Python 有多个库可以用于定时执行任务。以下是几个常用的库:
1. time + sleep虽然简单,但可以通过 time.sleep() 实现基本的定时任务。
import time
def task():
print("Task executed")
while True:
task()
time.sleep(60) # 每60秒执行一次 2. schedsched 是 Python 标准库中的一个简单调度器。
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
def task():
print("Task executed")
scheduler.enter(60, 1, task) # 每60秒执行一次
scheduler.enter(0, 1, task)
scheduler.run() 3. threading.Timerthreading.Timer 可以在单独的线程中定时执行任务。
import threading
def task():
print("Task executed")
threading.Timer(60.0, task).start()...点击查看剩余70%


