sched — Event scheduler

Source code: Lib/sched.py


The sched module defines a class which implements a general purpose event scheduler:sched模块定义了一个实现通用事件调度程序的类:

classsched.scheduler(timefunc=time.monotonic, delayfunc=time.sleep)

The scheduler class defines a generic interface to scheduling events. scheduler类定义了调度事件的通用接口。It needs two functions to actually deal with the “outside world” — timefunc should be callable without arguments, and return a number (the “time”, in any units whatsoever). 它需要两个函数来实际处理“外部世界”-timefunc应该可以无参数调用,并返回一个数字(“时间”,以任何单位)。The delayfunc function should be callable with one argument, compatible with the output of timefunc, and should delay that many time units. delayfunc函数应该可以用一个参数调用,与timefunc的输出兼容,并且应该延迟那么多时间单位。delayfunc will also be called with the argument 0 after each event is run to allow other threads an opportunity to run in multi-threaded applications.也将在每个事件运行后使用参数0调用,以允许其他线程有机会在多线程应用程序中运行。

Changed in version 3.3:版本3.3中更改: timefunc and delayfunc parameters are optional.timefuncdelayfunc参数是可选的。

Changed in version 3.3:版本3.3中更改: scheduler class can be safely used in multi-threaded environments.类可以在多线程环境中安全使用。

Example:示例:

>>> import sched, time
>>> s = sched.scheduler(time.time, time.sleep)
>>> def print_time(a='default'):
... print("From print_time", time.time(), a)
...
>>> def print_some_times():
... print(time.time())
... s.enter(10, 1, print_time)
... s.enter(5, 2, print_time, argument=('positional',))
... s.enter(5, 1, print_time, kwargs={'a': 'keyword'})
... s.run()
... print(time.time())
...
>>> print_some_times()
930343690.257
From print_time 930343695.274 positional
From print_time 930343695.275 keyword
From print_time 930343700.273 default
930343700.276

Scheduler Objects对象

scheduler instances have the following methods and attributes:实例具有以下方法和属性:

scheduler.enterabs(time, priority, action, argument=(), kwargs={})

Schedule a new event. 安排新的活动。The time argument should be a numeric type compatible with the return value of the timefunc function passed to the constructor. time参数应该是与传递给构造函数的timefunc函数的返回值兼容的数字类型。Events scheduled for the same time will be executed in the order of their priority. 安排在同一时间的事件将按其priority顺序执行。A lower number represents a higher priority.数字越低表示优先级越高。

Executing the event means executing action(*argument, **kwargs). 执行事件意味着执行action(*argument, **kwargs)argument is a sequence holding the positional arguments for action. argument是一个序列,用于保存操作的位置参数。kwargs is a dictionary holding the keyword arguments for action.是一个包含action的关键字参数的字典。

Return value is an event which may be used for later cancellation of the event (see cancel()).返回值是一个事件,可用于以后取消该事件(请参见cancel())。

Changed in version 3.3:版本3.3中更改: argument parameter is optional.参数是可选的。

Changed in version 3.3:版本3.3中更改: kwargs parameter was added.添加了kwargs参数。

scheduler.enter(delay, priority, action, argument=(), kwargs={})

Schedule an event for delay more time units. 将事件安排为delay更多时间单位。Other than the relative time, the other arguments, the effect and the return value are the same as those for enterabs().除了相对时间之外,其他参数、效果和返回值与enterabs()相同。

Changed in version 3.3:版本3.3中更改: argument parameter is optional.参数是可选的。

Changed in version 3.3:版本3.3中更改: kwargs parameter was added.添加了kwargs参数。

scheduler.cancel(event)

Remove the event from the queue. 从队列中删除事件。If event is not an event currently in the queue, this method will raise a ValueError.如果event不是队列中当前的事件,则此方法将引发ValueError

scheduler.empty()

Return True if the event queue is empty.如果事件队列为空,则返回True

scheduler.run(blocking=True)

Run all scheduled events. 运行所有计划的事件。This method will wait (using the delayfunc() function passed to the constructor) for the next event, then execute it and so on until there are no more scheduled events.该方法将等待(使用传递给构造函数的delayfunc()函数)下一个事件,然后执行它,依此类推,直到不再有计划的事件。

If blocking is false executes the scheduled events due to expire soonest (if any) and then return the deadline of the next scheduled call in the scheduler (if any).如果blockingfalse,则执行最快到期的计划事件(如果有),然后返回调度程序中下一个计划调用的截止日期(如果有)。

Either action or delayfunc can raise an exception. actiondelayfunc都可能引发异常。In either case, the scheduler will maintain a consistent state and propagate the exception. 无论哪种情况,调度程序都将保持一致状态并传播异常。If an exception is raised by action, the event will not be attempted in future calls to run().如果action引发异常,则在以后调用run()时将不会尝试该事件。

If a sequence of events takes longer to run than the time available before the next event, the scheduler will simply fall behind. 如果一系列事件的运行时间比下一个事件之前的可用时间长,则调度程序将落后。No events will be dropped; the calling code is responsible for canceling events which are no longer pertinent.不会放弃任何事件;调用代码负责取消不再相关的事件。

Changed in version 3.3:版本3.3中更改: blocking parameter was added.添加了blocking参数。

scheduler.queue

Read-only attribute returning a list of upcoming events in the order they will be run. 只读属性,按即将发生的事件的运行顺序返回这些事件的列表。Each event is shown as a named tuple with the following fields: time, priority, action, argument, kwargs.每个事件都显示为具有以下字段的命名元组:时间、优先级、操作、参数、kwargs。