这里介绍一下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. yii2 页面渲染方法解析

    render渲染.renderPartial渲染部分.renderContent.renderAjax.renderFile ① render显示view和layout ② renderPartial ...

  2. HTML5 touche vents drag to move & AF actionsheet by longTap

    $('img').on("touchstart",function(E){ //E.preventDefault();E.stopPropagation(); var el=thi ...

  3. Linux系统下wetty安装和使用说明

    1. Wetty简介 Wetty是使用Node.js和websockets开发的一个开源Web-based SSH.关于Web-based SSH的更多资料请参考https://en.wikipedi ...

  4. scp 链接文件的问题 + tar

    将本地的文件上传到服务器上,希望保持相对链接关系.(链接文件和被链接文件都在同一的目录下) 使用scp进行传输时,链接关系不会保存,所以需要解决下.(当然可以使用rsync或tar解决,但是rsync ...

  5. 本地文件到通过flume到kafka

    配置文件 cd /usr/app/flume1.6/conf vi flume-dirKakfa.properties #agent1 name agent1.sources=source1 agen ...

  6. 点击除元素以外的任意地方隐藏元素js

    比如想实现点击列表弹出筛选器,点击其他任意地方关闭筛选器,如图 该筛选器class名 $(document).click(function () { $(".subMenu").h ...

  7. MathType编辑粗体空心字的技巧

    在用MathType公式编辑器编辑公式时,由于不同的使用需要,会有不同的字体要求.如果是正式的论文中的公式,一般公式只要求数学的国际使用规定就可以了,而如果是用在ppt等这种演示的文稿中,所要用到的字 ...

  8. 使用springmvc,jsp,结合网页文本编辑器kindEditor实现基本博客编辑功能

    kindEditor官网:http://kindeditor.net/demo.php 个人实践: 为了在自己的项目中引入一个类似用户写博客的功能,在网上找到了kindeditor,真心又好又易用. ...

  9. 【转】细谈Redis和Memcached的区别

    Redis的作者Salvatore Sanfilippo曾经对这两种基于内存的数据存储系统进行过比较: Redis支持服务器端的数据操作:Redis相比Memcached来说,拥有更多的数据结构和并支 ...

  10. MySQL的sql_mode解析与设置,sql文件导入报错解决

    在往MySQL数据库中插入一组数据时,出错了!数据库无情了给我报了个错误:ERROR 1365(22012):Division by 0:意思是说:你不可以往数据库中插入一个 除数为0的运算的结果.于 ...