admin管理员组

文章数量:1531371

如果需要执行更复杂的任务调度,则可使用 Python 提供的 sched 模块。该模块提供了 sched.scheduler 类,该类代表一个任务调度器。

sched.scheduler(timefunc=time.monotonic, delayfunc=time.sleep) 构造器支持两个参数:

timefunc:该参数指定生成时间戳的时间函数,默认使用 time.monotonic 来生成时间戳。

delayfunc:该参数指定阻塞程序的函数,默认使用 time.sleep 函数来阻塞程序。

sched.scheduler 调度器支持如下常用属性和方法:

scheduler.enterabs(time, priority, action, argument=(), kwargs={}):指定在 time 时间点执行 action 函数,argument 和 kwargs 都用于向 action 函数传入参数,其中 argument 使用位置参数的形式传入参数,kwargs 使用关键字参数的形式传入参数。该方法返回一个 event,它可作为 cancel() 方法的参数用于取消该调度。priority 参数指定该任务的优先级,当在同一个时间点有多个任务需要执行时,优先级高(值越小代表优先级越高)的任务会优先执行。

1.scheduler.enter(delay, priority, action, argument=(),kwargs={}):该方法与上一个方法基本相同,只是 delay 参数用于指定多少秒之后执行 action 任务。

2.scheduler.cancel(event):取消任务。如果传入的 event 参数不是当前调度队列中的 event,

本文标签: 多线程Pythonschedule