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!‘)

时间: 2024-10-24 13:24:03

django定时任务python调度框架APScheduler使用详解的相关文章

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.schedul

定时任务框架APScheduler学习详解

APScheduler简介 在平常的工作中几乎有一半的功能模块都需要定时任务来推动,例如项目中有一个定时统计程序,定时爬出网站的URL程序,定时检测钓鱼网站的程序等等,都涉及到了关于定时任务的问题,第一时间想到的是利用time模块的time.sleep()方法使程序休眠来达到定时任务的目的,虽然这样也可以,但是总觉得不是那么的专业,^_^所以就找到了python的定时任务模块APScheduler: APScheduler基于Quartz的一个Python定时任务框架,实现了Quartz的所有功

python web框架企业实战详解(第六期)\第四课时-webpy&django

查找收集python的IDE,并分析各自优缺点:选择自己喜欢的IDE搭建各自的webpy和django环境,最后截屏就作业. PyCharm PyCharm是由JetBrains打造的一款Python IDE. PyCharm具备一般 Python IDE 的功能,比如:调试.语法高亮.项目管理.代码跳转.智能提示.自动完成.单元测试.版本控制等. 另外,PyCharm还提供了一些很好的功能用于Django开发,同时支持Google App Engine,更酷的是,PyCharm支持IronPy

python web框架企业实战详解(第六期)\第一课时-sorted&if&for

1.元组和列表的区别? 元组:用元括弧括起来的一组元素集合.其特点是内容丌可变,即一旦定义其长度和内容都是固定的:类似于C询言的数组. 列表:由中括弧括起来的包含一组元素的集合:其特点是长度和内容都可以改变.可以理解为java中的链表数组. 2.python中分割列表用什么方式? L = [0, False, 'l','AA','BBB'] print L[1:],L[:1],L[1:2],L[-1] 3.python中怎么进行多行注释? 采用三个单引号或者双引号括起来,如: def foo()

python web框架企业实战详解(第六期)\第二课时-pickle&__eq__

1.python的值传递和引用传递区别,哪些类型值传,哪些是引用传递? 值传递和引用传递区别:依据对象是否可变来确定 和其他语言不一样,传递参数的时候,python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是"传对象引用"的方式.实际上,这种方式相当于传值和传引用的一种综合.如果函数收到的是一个可变对象(比如字典或者列表)的引用,就能修改对象的原始值--相当于通过"传引用"来传递对象.如果函数收到的是一个不可变对象(比如数字.字符或者元组)的

python web框架企业实战详解(第六期)\第三课时-css&bootstrap

raw css: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style type="text/css"> p.ex1 { font:italic arial,sans-serif; } p.ex2 { font:italic bold 12px/3

python web框架企业实战详解(第六期)\第三课时-ajax&amp;jquery&amp;webpy

main.py __author__ = 'Liao' import web import time urls = ( '/gettime','gettime', '/(.*)', 'hello' ) app = web.application(urls, globals()) class gettime: def GET(self): asctime=time.asctime() print asctime return asctime def POST(self): return self.

SSH框架的整合详解

"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> SSH框架的整合详解 - super_YC的博客 - 博客频道 - CSDN.NET super_YC的博客 记录我生活的一点一滴!我很开心拥有这样一个自己心事的笔记本 目录视图 摘要视图 订阅 [活动]2017 CSDN博客专栏评选 &nbsp [5月书讯]流畅的P

Spring MVC 框架搭建及详解

一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.jar.commons-logging.jar.cglib-nodep-2.1_3.jar Hibernate 3.6.8:hibernate3.jar.hibernate-jpa-2.0-api-1.0.1.Final.jar.antlr-2.7.6.jar.commons-collections-3