django定时任务python调度框架APScheduler使用详解
# coding=utf-8
2 """
3 Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
4 intervals.
5 """
6
7 from datetime import datetime
8 import time
9 import os
10
11 from apscheduler.schedulers.background import BackgroundScheduler
12
13
14 def tick():
15 print('Tick! The time is: %s' % datetime.now())
16
17
18 if __name__ == '__main__':
19 scheduler = BackgroundScheduler()
20 scheduler.add_job(tick, 'interval', seconds=3) #间隔3秒钟执行一次
21 scheduler.start() #这里的调度任务是独立的一个线程
22 print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
23
24 try:
25 # This is here to simulate application activity (which keeps the main thread alive).
26 while True:
27 time.sleep(2) #其他任务是独立的线程执行
28 print('sleep!')
29 except (KeyboardInterrupt, SystemExit):
30 # Not strictly necessary if daemonic mode is enabled but should be done if possible
31 scheduler.shutdown()
32 print('Exit The Job!')

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

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

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

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

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

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

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

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

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

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

django定时任务python调度框架APScheduler使用详解的更多相关文章
- python调度框架APScheduler使用详解
# coding=utf-8 """ Demonstrates how to use the background scheduler to schedule a job ...
- 定时任务框架APScheduler学习详解
APScheduler简介 在平常的工作中几乎有一半的功能模块都需要定时任务来推动,例如项目中有一个定时统计程序,定时爬出网站的URL程序,定时检测钓鱼网站的程序等等,都涉及到了关于定时任务的问题,第 ...
- python爬虫框架scrapy实例详解
生成项目scrapy提供一个工具来生成项目,生成的项目中预置了一些文件,用户需要在这些文件中添加自己的代码.打开命令行,执行:scrapy st... 生成项目 scrapy提供一个工具来生成项目,生 ...
- Django框架 之 querySet详解
Django框架 之 querySet详解 浏览目录 可切片 可迭代 惰性查询 缓存机制 exists()与iterator()方法 QuerySet 可切片 使用Python 的切片语法来限制查询集 ...
- Python API 操作Hadoop hdfs详解
1:安装 由于是windows环境(linux其实也一样),只要有pip或者setup_install安装起来都是很方便的 >pip install hdfs 2:Client——创建集群连接 ...
- java的集合框架最全详解
java的集合框架最全详解(图) 前言:数据结构对程序设计有着深远的影响,在面向过程的C语言中,数据库结构用struct来描述,而在面向对象的编程中,数据结构是用类来描述的,并且包含有对该数据结构操作 ...
- Python安装、配置图文详解(转载)
Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境(I ...
- 【和我一起学python吧】Python安装、配置图文详解
Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境( ...
- Python中的高级数据结构详解
这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...
随机推荐
- poj 2324 Anniversary party(树形DP)
/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...
- 设计模式之观察者模式(php实现)
github地址:https://github.com/ZQCard/design_pattern /** * 当对象间存在一对多关系时,则使用观察者模式(Observer Pattern). * 比 ...
- win10 创建安卓模拟器及启动的方法
一打开 安卓 studio 然后点击AVD manager 创建一个模拟器 二 通过命令行快速启动模拟器 D:\Android\sdk\tools\emulator.exe -netdelay non ...
- crossapp里的位置设置
crossapp里有Frame.Center,这两种都是可以用来确定一个view的位置和大小. 不同点:Frame定位是以View的左上角为参照点,Center是以View的中心点为参照点 注意cro ...
- Shell--变量的显示与设置、环境变量、语系变量
1.变量的显示与设置:echo,unsetecho:显示一段文字,也可以读出变量内容并打印出来 格式echo $变量或者echo ${变量}语 法:echo [-neE][字符串]或 echo [- ...
- 2017.5.24 在intelliJ IDEA 中生成war包
1.勾选Build on make file -> project structure -> Artifacts 2.compile module "***" 选择项目 ...
- MyEclipse导入Hibernate出现Path must include project and resource;/project name
如图,在MyEclipse 2014以下版本中都没遇见这个问题. 在导入Hibernate框架的时候,可以说真的随缘,运气不好,明明配置全都没问题,还是连续几次失败,这个时候除了烧高香拜拜,也只能靠百 ...
- redis学习笔记——内存映射数据结构
内存映射数据结构 解决问题:当一个对象包含的元素数量并不多,或者元素本身的体积并不大时,使用代价高昂的内部数据结构并不是最好的办法. 内存映射数据结构是一系列经过特殊编码的字节序列,创建它们所消耗的内 ...
- ionic - 运行起来
更新时间: 2018-8-1 (首次更新) 1.首先下载python(至于为什么安装,看截图) https://www.python.org/downloads/release/python-370/ ...
- 《学习bash》笔记--进程处理
1.进程ID和作业编号 当通过附加&号后执行命令时.shell会响应例如以下: $ ls & [1] 3318 当中[1]是作业号,3318是进程号. 一个后台进程完毕时.shell ...