启动调度blockingscheduler.py

1、BlockingScheduler:当调度程序是您的流程中唯一运行的东西时使用 
2、只需调用start()调度程序即可启动调度 程序。对于除以外的调度程序BlockingScheduler,此调用将立即返回,您可以继续应用程序的初始化过程,可能会向调度程序添加作业。 
3、添加作业(add_job()) 
1、Blockingscheduler 这个是一个阻塞等待程序 
2、当调度程序是您的流程中唯一运行的东西时使用 
# 代码! 

 

from datetime import datetime 

import os 

import logging 

from apscheduler.schedulers.blocking import BlockingScheduler 

from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR 

  

basicConfig = {"level": logging.INFO, "format": "%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s", 

               "datefmt": '%Y-%m-%d %H:%M:%S', "filename": 'log1.BlockingScheduler', "filemode": "a"} 

kwargs={ 

    'apscheduler.jobstores.default': { 

        'type': 'sqlalchemy', 

        'url': 'postgres://postgres:123456@127.0.0.1:5433/postgres' 

    }, 

    'apscheduler.executors.default': { 

        'class': 'apscheduler.executors.pool:ThreadPoolExecutor', 

        'max_workers': '20' 

  

    }, 

    'apscheduler.executors.processpool': { 

        'type': 'processpool', 

        'max_workers': '5' 

    }, 

    'apscheduler.job_defaults.coalesce': 'false', 

    'apscheduler.job_defaults.max_instances': '3', 

    'apscheduler.timezone': 'Asia/Shanghai',  # 设置时区 ,存储桶时区 

} 

  

  

  

def my_listener(event): 

    if event.exception: 

        print('任务出错了!!!!!!') 

    else: 

        print('任务照常运行...') 

  

  

def tick(): 

    print('Tick! The time is: %s' % datetime.now()) 

  

  

if __name__ == '__main__': 

    logging.basicConfig(**basicConfig) 

    scheduler = BlockingScheduler(kwargs) 

    scheduler.add_job(tick, 'interval', seconds=3) 

    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) 

  

    try: 

        scheduler.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR) 

        scheduler._logger = logging 

        scheduler.start() 

    except (KeyboardInterrupt, SystemExit): 

        pass 
日志格式 红色标记 

basicConfig  日志配置 

Logging.basicConfig()初始化日志配置 

scheduler._logger = logging  加载日志配置 
侦听器 黄色标记 

任何代码都可能发生意外,关键是,发生意外了,如何第一时间知道,这才是公司最关心的,apscheduler已经为我们想到了这些。 
存储桶配置 绿色 

几年前学习得笔记搬过来 谷歌onenote 要vpn 好难受呀

 

 
posted @ 2021-12-13 20:21  Xingtxx  阅读(1153)  评论(0编辑  收藏  举报