小方哥哥 2020-02-18
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)