# 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. codechef AUG17 T2 Chef and Mover

    Chef and Mover Problem Code: CHEFMOVR Chef's dog Snuffles has so many things to play with! This time ...

  2. WebApi初探之路由配置

    本文介绍了ASP.NET Web API路由HTTP请求控制器. 如果你熟悉ASP.NET MVC,Web API路由是和MVC路由非常相似的.主要差别是Web API使用HTTP方法而不是URI路径 ...

  3. 获取Json对象的长度以及判断json对象是否为空

    (如有错敬请指点,以下是我工作中遇到并且解决的问题) = = = = = = = = = = = = = = = =  获取Json对象的长度  = = = = = = = = = = = = = = ...

  4. nginx 根据POST GET方法跳转

    location ~ /server/ {    proxy_pass_header   Server;    proxy_set_header Host $http_host;    proxy_r ...

  5. 如何让IE7,IE8支持css3

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 原理:在用ie浏览 ...

  6. CF987C Three displays【一维DP/类似最大子序列和】

    [链接]:CF987C [分析]:先求出每个s[i]后面比s[i]大的c[i]的最小值,然后枚举前两个数c(i),c(j)以及 j 后面递增且存在最小值的dp(j) [代码]: #include< ...

  7. Codeforces 940F Machine Learning (带修改莫队)

    题目链接  Codeforces Round #466 (Div. 2) Problem F 题意  给定一列数和若干个询问,每一次询问要求集合$\left\{c_{0}, c_{1}, c_{2}, ...

  8. servlet方法

    1.每一个Servlet都必须要实现Servlet接口,GenericServlet是个通用的.不特定于任何协议的Servlet,它实现了Servlet接口,而HttpServlet继承于Generi ...

  9. Python的并发并行[1] -> 线程[0] -> threading 模块

    threading模块 / threading Module 1 常量 / Constants Pass 2 函数 / Function 2.1 setprofile()函数 函数调用: thread ...

  10. Wannafly挑战赛22 A-计数器(gcd,裴蜀定理)

    原题地址 题目描述 有一个计数器,计数器的初始值为0,每次操作你可以把计数器的值加上a1,a2,...,an中的任意一个整数,操作次数不限(可以为0次),问计数器的值对m取模后有几种可能. 输入描述: ...