APScheduler是基於Quartz的一個Python定時任務框架,實現了Quartz的全部功能,使用起來十分方便。提供了基於日期、固定時間間隔以及crontab類型的任務,而且能夠持久化任務。基於這些功能,咱們能夠很方便的實現一個python定時任務系統。css
安裝過程很簡單,能夠基於pip和源碼。html
Pip install apscheduler==3.0.3python
或者下載源碼,運行命令:express
Python setup.py install框架
1: #coding=utf-8
2: from apscheduler.schedulers.blocking import BlockingScheduler
3: from datetime import datetime
4: import time
5: import os
6:
7: def tick():
8: print('Tick! The time is: %s' % datetime.now())
9:
10: if __name__ == '__main__':
11: scheduler = BlockingScheduler()
12: scheduler.add_job(tick,'cron', second='*/3', hour='*')
13: print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
14: try:
15: scheduler.start()
16: except (KeyboardInterrupt, SystemExit):
17: scheduler.shutdown()
18:
19:
20:
Expressionspa |
Fieldcode |
Descriptionorm |
*htm |
anycrontab |
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 |