码迷,mamicode.com
首页 > 其他好文 > 详细

django 定时脚本

时间:2018-05-13 20:00:17      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:done   exce   add   second   其他   daemon   imp   min   调度   

python 第三方定时执行
from datetime import datetime
import time
import os

from apscheduler.schedulers.background import BackgroundScheduler


def tick():
    print(‘Tick! The time is: %s‘ % datetime.now())


scheduler = BackgroundScheduler()
scheduler.add_job(tick, ‘interval‘, minutes=1)
scheduler.start()    #这里的调度任务是独立的一个线程

if __name__ == ‘__main__‘:
    scheduler = BackgroundScheduler()
    scheduler.add_job(tick, ‘interval‘, seconds=3)
    # scheduler.add_job(tick, ‘date‘, run_date=‘2016-02-14 15:01:05‘)#在指定的时间,只执行一次
    scheduler.start()    #这里的调度任务是独立的一个线程
    print(‘Press Ctrl+{0} to exit‘.format(‘Break‘ if os.name == ‘nt‘ else ‘C‘))

    try:
        # This is here to simulate application activity (which keeps the main thread alive).
        while True:
            time.sleep(2)    #其他任务是独立的线程执行
            print(‘sleep!‘)
    except (KeyboardInterrupt, SystemExit):
        # Not strictly necessary if daemonic mode is enabled but should be done if possible
        scheduler.shutdown()
        print(‘Exit The Job!‘)

  

django 定时脚本

标签:done   exce   add   second   其他   daemon   imp   min   调度   

原文地址:https://www.cnblogs.com/flash55/p/9032960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!