最近在研究python調(diào)度框架APScheduler使用的路上,那么今天也算個學(xué)習(xí)筆記吧!
# coding=utf-8"""Demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals."""from datetime import datetimeimport timeimport osfrom apscheduler.schedulers.background import BackgroundSchedulerdef tick(): print('Tick! The time is: %s' % datetime.now())if __name__ == '__main__': scheduler = BackgroundScheduler() scheduler.add_job(tick, 'interval', seconds=3) #間隔3秒鐘執(zhí)行一次 scheduler.start() #這里的調(diào)度任務(wù)是獨(dú)立的一個線程 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) #其他任務(wù)是獨(dú)立的線程執(zhí)行 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!')非阻塞調(diào)度,在指定的時間執(zhí)行一次
# coding=utf-8"""Demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals."""from datetime import datetimeimport timeimport osfrom apscheduler.schedulers.background import BackgroundSchedulerdef tick(): print('Tick! The time is: %s' % datetime.now())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') #在指定的時間,只執(zhí)行一次 scheduler.start() #這里的調(diào)度任務(wù)是獨(dú)立的一個線程 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) #其他任務(wù)是獨(dú)立的線程執(zhí)行 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!')非阻塞的方式,采用cron的方式執(zhí)行
# coding=utf-8"""Demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals."""from datetime import datetimeimport timeimport osfrom apscheduler.schedulers.background import BackgroundSchedulerdef tick(): print('Tick! The time is: %s' % datetime.now())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.add_job(tick, 'cron', day_of_week='6', second='*/5') ''' year (int|str)
主站蜘蛛池模板:
扎鲁特旗|
麻城市|
遂川县|
神农架林区|
钦州市|
永寿县|
农安县|
百色市|
温州市|
永城市|
郑州市|
郁南县|
神农架林区|
开封市|
巴林左旗|
宁海县|
平度市|
吉首市|
唐海县|
泸溪县|
丹棱县|
马公市|
建宁县|
龙山县|
西青区|
昌黎县|
玛纳斯县|
洮南市|
东港市|
奈曼旗|
青铜峡市|
武山县|
盐津县|
凤庆县|
和龙市|
龙川县|
林周县|
镇巴县|
沾益县|
新龙县|
北碚区|