Python的APScheduler模块

来源:互联网 发布:node forever 重启 编辑:程序博客网 时间:2024/06/05 10:01

APScheduler:是一个任务定时执行的模块,定时调度自己的任务,比系统提供的定时服务灵活很多


可以采用两种方式添加任务,调用add_job()方法或使用scheduled_job()装饰器。

调用add_job方法:

[python] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. import datetime  
  2. from apscheduler.schedulers.blocking import BlockingScheduler  
  3.   
  4. scheduler = BlockingScheduler()  
  5.   
  6. def test():  
  7.     print "now is '%s' " % datetime.datetime.now()  
  8.   
  9. scheduler.add_job(test, "cron", second="*/3")  
  10.   
  11. try:  
  12.     scheduler.start()  
  13. except (KeyboardInterrupt, SystemExit):  
  14.     scheduler.shutdown()  

使用装饰器:

[python] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. import datetime  
  2. from apscheduler.schedulers.blocking import BlockingScheduler  
  3.   
  4. scheduler = BlockingScheduler()  
  5.  
  6.  
  7. @scheduler.scheduled_job("cron", second="*/3")  
  8. def test():  
  9.     print "now is '%s' " % datetime.datetime.now()  
  10.   
  11. try:  
  12.     scheduler.start()  
  13. except (KeyboardInterrupt, SystemExit):  
  14.     scheduler.shutdown(  

cron表达式说明 

Expression

Field

Description

*

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




官方文档:http://apscheduler.readthedocs.org/en/latest/index.html

0 0
原创粉丝点击