python定时任务框架
from datetime import datetimeimport osimport timefrom apscheduler.schedulers.blocking import BlockingSchedulerfrom apscheduler.schedulers.background import BackgroundSchedulerdef tick(): print(‘tick! time is {}‘.format(datetime.now())) with open(‘1.txt‘, ‘a‘) as f: f.write(‘1‘)if __name__ == ‘__main__‘: # scheduler = BlockingScheduler() # 这是最简单的调度器,阻塞,相当于守护进程 # scheduler.add_job(tick, ‘interval‘, seconds=3) # 没3s执行一次 # scheduler.add_job(tick, ‘cron‘, hour=18, minute=37) # 每天的18:23执行一次 # scheduler.add_job(tick, ‘cron‘, minute=‘*‘, second=‘*‘) # 每秒执行一次 # scheduler.add_job(tick, ‘cron‘, second=‘*/3‘) # 每3s执行一次 scheduler = BackgroundScheduler() # 后台运行,不阻塞, 住进程结束任务结束 scheduler.add_job(tick, ‘cron‘, second=‘*/3‘) print(‘press ctrl+{0} to exit‘.format(‘break‘ if os.name == ‘nt‘ else ‘c ‘)) try: scheduler.start() except: pass while 1: time.sleep(4)
相关推荐
doubinning 2019-12-13
leoaran 2019-11-04
wordmhg 2019-10-29
viplinux 2019-06-29
chaigang 2019-04-09
ManyPeng 2019-03-30
CandyGL 2014-11-19
heyiaiqing 2018-09-27
cairencong 2018-09-08
xinhao 2018-05-21
爱是永不止息 2016-12-08
巴壁虎 2017-07-06
LoveLifeLoveTech 2016-12-08
liushuibufuqin 2013-05-16
Haopython 2019-02-19