Python中的调度模块
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)
调度程序实例可用的基本方法
- scheduler.enter():可以安排事件在延迟后运行,或者在特定时间运行。要延迟安排它们,请使用
enter()
方法。Syntax: scheduler.enter(delay, priority, action, argument=(), kwargs={})
Parameters:
delay: A number representing the delay time
priority: Priority value
action: Calling function
argument: A tuple of arguments例子:
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
- scheduler.enterabs()
enterabs()
时将一个事件添加到调度器的内部队列中,当调用调度器的run()
方法时,调度器队列中的条目会被一一执行。Syntax: scheduler.enterabs(time, priority, action, argument=(), kwargs={})
Parameters:
time: Time at which the event/action has to be executed
priority: The priority of the event
action: The function that constitutes an event
argument: Positional arguments to the event function
kwargs: A dictionary of keyword arguments to the event function例子:
# 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
- scheduler.cancel()从队列中移除事件。如果事件不是当前队列中的事件,则此方法将引发
ValueError
。Syntax: scheduler.cancel(event)
Parameter:
event: The event that should be removed.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
- scheduler.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