# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
""" from datetime import datetime
import time
import os from apscheduler.schedulers.background import BackgroundScheduler def tick():
print('Tick! The time is: %s' % datetime.now()) if __name__ == '__main__':
scheduler = BackgroundScheduler()
scheduler.add_job(tick, 'interval', seconds=3)  #间隔3秒钟执行一次
scheduler.start() #这里的调度任务是独立的一个线程
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2) #其他任务是独立的线程执行
print('sleep!')
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')

非阻塞调度,在指定的时间执行一次

 # coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
""" from datetime import datetime
import time
import os from apscheduler.schedulers.background import BackgroundScheduler def tick():
print('Tick! The time is: %s' % datetime.now()) if __name__ == '__main__':
scheduler = BackgroundScheduler()
#scheduler.add_job(tick, 'interval', seconds=3)
scheduler.add_job(tick, 'date', run_date='2016-02-14 15:01:05')  #在指定的时间,只执行一次
scheduler.start() #这里的调度任务是独立的一个线程
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2) #其他任务是独立的线程执行
print('sleep!')
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')

非阻塞的方式,采用cron的方式执行

 # coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
""" from datetime import datetime
import time
import os from apscheduler.schedulers.background import BackgroundScheduler def tick():
print('Tick! The time is: %s' % datetime.now()) if __name__ == '__main__':
scheduler = BackgroundScheduler()
#scheduler.add_job(tick, 'interval', seconds=3)
#scheduler.add_job(tick, 'date', run_date='2016-02-14 15:01:05')
scheduler.add_job(tick, 'cron', day_of_week='', second='*/5')
'''
year (int|str) – 4-digit year
month (int|str) – month (1-12)
day (int|str) – day of the (1-31)
week (int|str) – ISO week (1-53)
day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
hour (int|str) – hour (0-23)
minute (int|str) – minute (0-59)
second (int|str) – second (0-59) start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)
end_date (datetime|str) – latest possible date/time to trigger on (inclusive)
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone) * any Fire on every value
*/a any Fire every a values, starting from the minimum
a-b any Fire on any value within the a-b range (a must be smaller than b)
a-b/c any Fire every c values within the a-b range
xth y day Fire on the x -th occurrence of weekday y within the month
last x day Fire on the last occurrence of weekday x within the month
last day Fire on the last day within the month
x,y,z any Fire on any matching expression; can combine any number of any of the above expressions
'''
scheduler.start() #这里的调度任务是独立的一个线程
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2) #其他任务是独立的线程执行
print('sleep!')
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')

阻塞的方式,间隔3秒执行一次

 # coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
""" from datetime import datetime
import os from apscheduler.schedulers.blocking import BlockingScheduler def tick():
print('Tick! The time is: %s' % datetime.now()) if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(tick, 'interval', seconds=3) print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try:
scheduler.start() #采用的是阻塞的方式,只有一个线程专职做调度的任务
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')

采用阻塞的方法,只执行一次

 # coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
""" from datetime import datetime
import os from apscheduler.schedulers.blocking import BlockingScheduler def tick():
print('Tick! The time is: %s' % datetime.now()) if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(tick, 'date', run_date='2016-02-14 15:23:05') print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try:
scheduler.start() #采用的是阻塞的方式,只有一个线程专职做调度的任务
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')

采用阻塞的方式,使用cron的调度方法

 # coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
""" from datetime import datetime
import os from apscheduler.schedulers.blocking import BlockingScheduler def tick():
print('Tick! The time is: %s' % datetime.now()) if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(tick, 'cron', day_of_week='', second='*/5')
'''
year (int|str) – 4-digit year
month (int|str) – month (1-12)
day (int|str) – day of the (1-31)
week (int|str) – ISO week (1-53)
day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
hour (int|str) – hour (0-23)
minute (int|str) – minute (0-59)
second (int|str) – second (0-59) start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)
end_date (datetime|str) – latest possible date/time to trigger on (inclusive)
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone) * any Fire on every value
*/a any Fire every a values, starting from the minimum
a-b any Fire on any value within the a-b range (a must be smaller than b)
a-b/c any Fire every c values within the a-b range
xth y day Fire on the x -th occurrence of weekday y within the month
last x day Fire on the last occurrence of weekday x within the month
last day Fire on the last day within the month
x,y,z any Fire on any matching expression; can combine any number of any of the above expressions
''' print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try:
scheduler.start() #采用的是阻塞的方式,只有一个线程专职做调度的任务
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')

python调度框架APScheduler使用详解的更多相关文章

  1. django定时任务python调度框架APScheduler使用详解

    # coding=utf-8 2 """ 3 Demonstrates how to use the background scheduler to schedule a ...

  2. 定时任务框架APScheduler学习详解

    APScheduler简介 在平常的工作中几乎有一半的功能模块都需要定时任务来推动,例如项目中有一个定时统计程序,定时爬出网站的URL程序,定时检测钓鱼网站的程序等等,都涉及到了关于定时任务的问题,第 ...

  3. python爬虫框架scrapy实例详解

    生成项目scrapy提供一个工具来生成项目,生成的项目中预置了一些文件,用户需要在这些文件中添加自己的代码.打开命令行,执行:scrapy st... 生成项目 scrapy提供一个工具来生成项目,生 ...

  4. Django框架 之 querySet详解

    Django框架 之 querySet详解 浏览目录 可切片 可迭代 惰性查询 缓存机制 exists()与iterator()方法 QuerySet 可切片 使用Python 的切片语法来限制查询集 ...

  5. Python API 操作Hadoop hdfs详解

    1:安装 由于是windows环境(linux其实也一样),只要有pip或者setup_install安装起来都是很方便的 >pip install hdfs 2:Client——创建集群连接 ...

  6. java的集合框架最全详解

    java的集合框架最全详解(图) 前言:数据结构对程序设计有着深远的影响,在面向过程的C语言中,数据库结构用struct来描述,而在面向对象的编程中,数据结构是用类来描述的,并且包含有对该数据结构操作 ...

  7. Python安装、配置图文详解(转载)

    Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境(I ...

  8. 【和我一起学python吧】Python安装、配置图文详解

     Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境( ...

  9. Python中的高级数据结构详解

    这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...

随机推荐

  1. 将GDB中的输出定向到文件

    1 set args >output.log 三种方法,一种通过tee在启动时重定向: 1 gdb |tee -a file 第二种在run时加入: 1 run <input.txt &g ...

  2. springboot webapi 支持跨域 CORS

    1.创建corsConfig 配置文件 @Configuration public class corsConfig { @Autowired varModel varModel_; @Bean pu ...

  3. 第十二届北航程序设计竞赛决赛网络同步赛 B题 前前前世(数论推导 + DP)

    题目链接  2016 BUAA-Final Problem B 考虑一对可行的点$(x, y)$ 根据题意,设$x = ak + 1,y = bk + 1$ 又因为$x$是$y$的祖先的祖先的祖先,所 ...

  4. 差分【p3948】 数据结构

    顾z 你没有发现两个字里的blog都不一样嘛 qwq 题目描述-->p3948 数据结构 分析 其实这题完全没有分析的 qwq. 只是因为写了差分数组相关知识,所以顺便写一下题解 qwq. 对于 ...

  5. Linux的软连接和硬连接

    1.Linux链接概念 Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接] 硬连接指通过索引 ...

  6. 四. Java继承和多态1. 继承的概念与实现

    继承是类与类之间的关系,是一个很简单很直观的概念,与现实世界中的继承(例如儿子继承父亲财产)类似. 继承可以理解为一个类从另一个类获取方法和属性的过程.如果类B继承于类A,那么B就拥有A的方法和属性. ...

  7. 单条sql性能分析与优化

    性能分析 1. explain 查看sql执行计划,得出索引使用情况等信息 2. show profiling 查看sql所有执行步骤以及用时,使用步骤如下 1)开启性能剖析 mysql> se ...

  8. VisualStudio Shell简介 — 集成插件

    Visual Studio Shell只是提供了一个内核,我们还需要在其基础上补充功能,从而实现我们自己的IDE.Visual Studio Shell的插件开发和Visual Studio插件开发是 ...

  9. SQL 序号列ROW_NUMBER,RANK,DENSE_RANK、NTILE

    原文:SQL 序号列ROW_NUMBER,RANK,DENSE_RANK.NTILE SQL 2005新增加相关函数 : ROW_NUMBER,RANK,DENSE_RANK.NTILE 窗口函数 O ...

  10. 用CSS3产生动画效果

    相关属性: @keyframes规则:定义动画 语法:@keyframes animationname{keyframes-selector {CSS-style;}} animationname:动 ...