Python的APScheduler模块

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值