🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](queue.xhtml "queue --- 一个同步的队列类") | - [上一页](subprocess.xhtml "subprocess --- 子进程管理") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [并发执行](concurrency.xhtml) » - $('.inline-search').show(0); | # [`sched`](#module-sched "sched: General purpose event scheduler.") --- 事件调度器 **源码:** [Lib/sched.py](https://github.com/python/cpython/tree/3.7/Lib/sched.py) \[https://github.com/python/cpython/tree/3.7/Lib/sched.py\] - - - - - - [`sched`](#module-sched "sched: General purpose event scheduler.") 模块定义了一个实现通用事件调度程序的类: *class* `sched.``scheduler`(*timefunc=time.monotonic*, *delayfunc=time.sleep*)[`scheduler`](#sched.scheduler "sched.scheduler") 类定义了一个调度事件的通用接口。 它需要两个函数来实际处理“外部世界” —— *timefunc* 应该可以在没有参数的情况下调用,并返回一个数字(“时间”,以任何单位表示)。 如果 time.monotonic 不可用,则 *timefunc* 默认为 time.time。 *delayfunc* 函数应该用一个参数可调用,与 *timefunc* 的输出兼容,并且应该延迟那么多时间单位。 每个事件运行后,还会使用参数 `0` 调用 *delayfunc*,以允许其他线程有机会在多线程应用程序中运行。 在 3.3 版更改: *timefunc* 和 *delayfunc* 参数是可选的。 在 3.3 版更改: [`scheduler`](#sched.scheduler "sched.scheduler") 类可以安全的在多线程环境中使用。 示例: ``` >>> 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`](#sched.scheduler "sched.scheduler") 实例拥有以下方法和属性: `scheduler.``enterabs`(*time*, *priority*, *action*, *argument=()*, *kwargs={}*)安排一个新事件。 *time* 参数应该有一个数字类型兼容的返回值,与传递给构造函数的 *timefunc* 函数的返回值兼容。 计划在相同 *time* 的事件将按其 *priority* 的顺序执行。 数字越小表示优先级越高。 执行事件意为执行 `action(*argument, **kwargs)`。 *argument* 是包含有 *action* 的位置参数的序列。 *kwargs* 是包含 *action* 的关键字参数的字典。 返回值是一个事件,可用于以后取消事件( 参见 [`cancel()`](#sched.scheduler.cancel "sched.scheduler.cancel") )。 在 3.3 版更改: *argument* 参数是可选的。 3\.3 新版功能: 添加了 *kwargs* 形参。 `scheduler.``enter`(*delay*, *priority*, *action*, *argument=()*, *kwargs={}*)安排延后 *delay* 时间单位的事件。 除了相对时间,其他参数、效果和返回值与 [`enterabs()`](#sched.scheduler.enterabs "sched.scheduler.enterabs") 的相同。 在 3.3 版更改: *argument* 参数是可选的。 3\.3 新版功能: 添加了 *kwargs* 形参。 `scheduler.``cancel`(*event*)从队列中删除事件。 如果 *event* 不是当前队列中的事件,则此方法将引发 [`ValueError`](exceptions.xhtml#ValueError "ValueError")。 `scheduler.``empty`()如果事件队列为空,则返回真值。 `scheduler.``run`(*blocking=True*)运行所有预定事件。 此方法将等待(使用传递给构造函数的 `delayfunc()` 函数)进行下一个事件,然后执行它,依此类推,直到没有更多的计划事件。 如果 *blocking* 为false,则执行由于最快到期(如果有)的预定事件,然后在调度程序中返回下一个预定调用的截止时间(如果有)。 *action* 或 *delayfunc* 都可以引发异常。 在任何一种情况下,调度程序都将保持一致状态并传播异常。 如果 *action* 引发异常,则在将来调用 [`run()`](#sched.scheduler.run "sched.scheduler.run") 时不会尝试该事件。 如果一系列事件的运行时间比下一个事件之前的可用时间长,那么调度程序将完全落后。 不会发生任何事件;调用代码负责取消不再相关的事件。 3\.3 新版功能: 添加了 *blocking* 形参。 `scheduler.``queue`只读属性按照将要运行的顺序返回即将发生的事件列表。 每个事件都显示为 [named tuple](../glossary.xhtml#term-named-tuple) ,包含以下字段:time、priority、action、argument、kwargs。 ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](queue.xhtml "queue --- 一个同步的队列类") | - [上一页](subprocess.xhtml "subprocess --- 子进程管理") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [并发执行](concurrency.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。