Sched模块是标准库,可用于创建机器人和其他监控和自动化应用程序。 sched 模块实现了一个通用事件调度程序,用于在特定时间运行任务。它提供了类似于Windows或Linux中的任务调度程序的工具,但主要优点是与Python自己的sched模块平台差异可以忽略。
例子:
# Python program for Creating
# an event scheduler
import sched
import time
# Creating an instance of the
# scheduler class
scheduler = sched.scheduler(time.time,
time.sleep)
调度程序实例可用的基本方法
- 调度程序.enter():事件可以安排在延迟后或在特定时间运行。为了延迟安排它们,
enter()
使用方法。
用法: scheduler.enter(delay, priority, action, argument=(), kwargs={})
参数:
delay:代表延迟时间的数字
priority:优先值
action:调用函数
argument:参数元组例子:
import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time # and name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # first event with delay of # 1 second e1 = scheduler.enter(1, 1, print_event, ('1 st', )) # second event with delay of # 2 seconds e1 = scheduler.enter(2, 1, print_event, (' 2nd', )) # executing the events scheduler.run()
输出:
START: 1580389814.152131 EVENT: 1580389815.1548214 1 st EVENT: 1580389816.1533117 2nd
- 调度程序.enterabs()
enterabs()
time 将事件添加到调度程序的内部队列中,因为run()
调用调度器的方法,调度器队列中的条目被一一执行。
用法: scheduler.enterabs(time, priority, action, argument=(), kwargs={})
参数:
time:必须执行事件/操作的时间
priority:事件的优先级
action:构成事件的函数
argument:事件函数的位置参数
kwargs:事件函数的关键字参数字典例子:
# library imported import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time and # name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # event x with delay of 1 second # enters queue using enterabs method e_x = scheduler.enterabs(time.time()+1, 1, print_event, argument = ("Event X", )); # executing the events scheduler.run()
输出:
START: 1580389960.5845037 EVENT: 1580389961.5875661 Event X
- 调度程序.cancel()从队列中删除该事件。如果该事件不是当前队列中的事件,则此方法将引发
ValueError
.
用法: scheduler.cancel(event)
参数:
event:应删除的事件。import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time and # name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # first event with delay # of 1 second e1 = scheduler.enter(1, 1, print_event, ('1 st', )) # second event with delay # of 2 seconds e2 = scheduler.enter(2, 1, print_event, (' 2nd', )) # removing 1st event from # the event queue scheduler.cancel(e1) # executing the events scheduler.run()
输出:
START: 1580390119.54074 EVENT: 1580390121.5439944 2nd
- 调度程序.empty()如果事件队列为空,则返回 True。这不需要争论。
例子:
import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time # and name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # checking if event queue is # empty or not print(scheduler.empty()) # event entering into queue e1 = scheduler.enter(2, 1, print_event, ('1 st', )) # checking if event queue is # empty or not print(scheduler.empty()) # executing the events scheduler.run()
输出:
START: 1580390318.1343799 True False EVENT: 1580390320.136075 1 st
- scheduler.queue只读属性,按事件运行顺序返回即将发生的事件的列表。每个事件都显示为具有以下字段的命名元组:时间、优先级、操作、参数、kwargs。
# library imported import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time # and name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # event entering into queue e1 = scheduler.enter(2, 1, print_event, ('1 st', )) # printing the details of # upcoming events in event queue print(scheduler.queue) # executing the events scheduler.run()
输出:
START: 1580390446.8743565
[Event(time=1580390448.8743565, priority=1, action=, argument=(‘1 st’, ), kwargs={})]
EVENT: 1580390448.876916 1 st
相关用法
- Python Scipy stats.alpha()用法及代码示例
- Python Scipy stats.arcsine()用法及代码示例
- Python Scipy stats.bayes_mvs()用法及代码示例
- Python Scipy stats.beta()用法及代码示例
- Python Scipy stats.betaprime()用法及代码示例
- Python Scipy stats.binned_statistic()用法及代码示例
- Python Scipy stats.binned_statistic_2d()用法及代码示例
- Python Scipy stats.binned_statistic_dd()用法及代码示例
- Python Scipy stats.bradford()用法及代码示例
- Python Scipy stats.burr()用法及代码示例
- Python Scipy stats.cauchy()用法及代码示例
- Python Scipy stats.chi()用法及代码示例
- Python Scipy stats.cosine()用法及代码示例
- Python Scipy stats.cumfreq()用法及代码示例
- Python Scipy stats.describe()用法及代码示例
- Python Scipy stats.dgamma()用法及代码示例
- Python Scipy stats.dweibull()用法及代码示例
- Python Scipy stats.erlang()用法及代码示例
- Python Scipy stats.exponpow()用法及代码示例
- Python Scipy stats.exponweib()用法及代码示例
- Python Scipy stats.f()用法及代码示例
- Python Scipy stats.fatiguelife()用法及代码示例
- Python Scipy stats.fisk()用法及代码示例
- Python Scipy stats.foldcauchy()用法及代码示例
- Python Scipy stats.foldnorm()用法及代码示例
注:本文由纯净天空筛选整理自rakshitarora大神的英文原创作品 Sched module in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。