这里介绍一下python中关于定时器的一些使用,包括原生的sche包和第三方框架APScheduler的实现。流年未亡,夏日已尽。种花的人变成了看花的人,看花的人变成了葬花的人。

python中的sche模块

python中的sch有模块提供了定时器任务的功能,在3.3版本之后sche模块里面的scheduler类在多线程的环境中是安全的。以下是一个案例

import sched, time

sche = sched.scheduler(time.time, time.sleep)

def print_time(a='default'):
print('From print_time', time.time(), a) def print_some_time():
print(time.time())
# 10是delay单位是毫秒, 1代表优先级
sche.enter(10, 1, print_time)
sche.enter(5, 2, print_time, argument=('positional',))
sche.enter(5, 1, print_time, kwargs={'a': 'keyword'})
sche.run()
print(time.time()) print_some_time()

运行的打印结果如下:

1511139390.598038
From print_time 1511139395.5982432 keyword
From print_time 1511139395.5982432 positional
From print_time 1511139400.5984359 default
1511139400.5984359

查看scheduler.enter的源码,可以看到它实际调用的是enterabs方法。

def enter(self, delay, priority, action, argument=(), kwargs=_sentinel):
"""A variant that specifies the time as a relative time. This is actually the more commonly used interface. """
time = self.timefunc() + delay
return self.enterabs(time, priority, action, argument, kwargs)

而对于enterabs方法的argument和kwargs的含义,官方文档的解释如下:它们可以作为action的参数,也就是上述例子中的print_time方法的参数

argument is a sequence holding the positional arguments for action. kwargs is a dictionary holding the keyword arguments for action.

APScheduler的使用

APScheduler的安装:pip install apscheduler。

一、第一个APScheduler的使用案例

每隔一秒打印出Hello World的字符。

from apscheduler.schedulers.blocking import BlockingScheduler
def my_job():
print('Hello World') sched = BlockingScheduler()
sched.add_job(my_job, 'interval', seconds=1)
sched.start()

友情链接

python框架---->APScheduler的使用的更多相关文章

  1. python 定时任务框架apscheduler

    文章目录 安装 基本概念介绍 调度器的工作流程 实例1 -间隔性任务 实例2 - cron 任务 配置调度器 方法一 方法二 方法三: 启动调度器 方法一:使用默认的作业存储器: 方法二:使用数据库作 ...

  2. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

  3. 【python】Python框架、库和软件资源大全

    很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世界各地的程序员们都能够贡献他们的代码与创新. Python就是这样一门受到全世界各地开源社区支持的语言. ...

  4. Python框架、库和软件资源大全(整理篇)

    有少量修改,请访问原始链接.PythonWIn的exe安装包;http://www.lfd.uci.edu/~gohlke/pythonlibs/ 原文链接:codecloud.net/python- ...

  5. python 定时任务APScheduler 使用介绍

    python 定时任务APScheduler 使用介绍   介绍: APScheduler的全称是Advanced Python Scheduler.它是一个轻量级的 Python 定时任务调度框架. ...

  6. python框架之django

    python框架之django 本节内容 web框架 mvc和mtv模式 django流程和命令 django URL django views django temple django models ...

  7. android模拟器(genymotion)+appium+python 框架执行基本原理(目前公司自己写的)

    android模拟器(genymotion)+appium+python 框架执行的基本过程: 1.Push.initDate(openid)方法     //业务数据初始化 1.1   v5db.p ...

  8. 10个用于Web开发的最好 Python 框架

    Python 是一门动态.面向对象语言.其最初就是作为一门面向对象语言设计的,并且在后期又加入了一些更高级的特性.除了语言本身的设计目的之外,Python标准 库也是值得大家称赞的,Python甚至还 ...

  9. 第六篇:web之python框架之django

    python框架之django   python框架之django 本节内容 web框架 mvc和mtv模式 django流程和命令 django URL django views django te ...

随机推荐

  1. 使用mysqldiff生成两个数据库结构不同的脚本

    1,全库比较各个表的不同,并输出到文件 mysqldiff --server1=root:root@localhost --server2=root:root@localhost --difftype ...

  2. openssl创建自己的CA certificate

    Create a Certificate Authority private key (this is your most important key): $ openssl req -new -ne ...

  3. PHP进阶。

    老手段,百度“PHP进阶” 不过,今天运气不错,搜到一个“PHP特级内容讲解”,地址是:http://wenku.baidu.com/course/view/fd8e591b6bd97f192279e ...

  4. XAMPP permissions on Mac OS X

    $ cd /Applications $ XAMPP/ 注意: 改变的是XAMPP目录,而不是htdocs ref: http://stackoverflow.com/questions/904697 ...

  5. mocha框架下,异步测试代码错误造成的问题----用例超时错误

    今天用抹茶(mocha)做个测试,发现有一个测试项目总是超时: describe("DbFactory functions",function(){ it("query ...

  6. The difference between the request time and the current time is too large.阿里云oss上传图片报错

    The difference between the request time and the current time is too large. 阿里云oss上传图片的时候报错如上, 解决办法,把 ...

  7. vue父组件中获取子组件中的数据

    <FormItem label="上传头像" prop="image"> <uploadImg :width="150" ...

  8. 【scala】 scala xml 处理(⑨)

    1.scala 处理xml 2. 获取属性 3.修改节点 4.遍历 5.模式匹配 6.命名空间 7.文件加载 import scala.xml._ /** * @author xwolf * @sin ...

  9. Explore Basic Behavior of the TurtleBot ---3

    原创博文:转载请标明出处(周学伟):http://www.cnblogs.com/zxouxuewei/tag/ Introduction 此示例帮助您使用turtlebot的自主性. 驱动机器人向前 ...

  10. Objective-C 协议和运行时检查方法、类是否存在

    协议的声明: // // Person.h // TestOC01 // // Created by xinye on 13-10-23. // Copyright (c) 2013年 xinye. ...