apscheduler 官方文档:http://apscheduler.readthedocs.io/en/latest/
写一个后台定时任务,一般2个选择,一个是apscheduler,一个celery,apscheduler比较直观简单。
安装:pip install apscheduler
简单例子:-------------------------------------------------------------------------------------------
>>> from apscheduler.schedulers.blocking import BlockingScheduler
>>> import datetime
>>> def say_now():
... print datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
...
>>> scheduler = BlockingScheduler()
>>> scheduler.add_job(func=say_now, trigger='cron', second='*/5') #可以指定各种定时执行规则,非常全,详情见官方文档 http://apscheduler.readthedocs.io/en/latest/
<Job (id=c1d3e4e331fd4f63b0a551f64c985354 name=say_now)>
>>> scheduler.start()
2018-08-12 18:57:10
2018-08-12 18:57:15
2018-08-12 18:57:20
2018-08-12 18:57:25
2018-08-12 18:57:30
2018-08-12 18:57:35
-------------------------------------------------------------------------------------------