Python定时任务APScheduler框架的使用

来源:互联网 发布:手机如何切换软件 编辑:程序博客网 时间:2024/06/04 18:07

                       Python定时任务APScheduler框架的使用

    测试使用APScheduler框架的后台调度器运行写文件job不生效,但是Blocking调度器可正常工作,不知道怎么回事了。。。
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time    : 2017/11/26 下午9:42# @Author  : 冷月孤心# @Mail    : codenutter@foxmail.com# @File    : apscheduler_demo.pyfrom apscheduler.schedulers.blocking import BlockingSchedulerfrom apscheduler.schedulers.background import BackgroundSchedulerfrom datetime import datetimeimport loggingdef init_logger(filename, level, format, datefmt):    """    初始化日志记录器    :param filename: 日志文件名    :param level: 日志级别    :param format: 格式化输出    :param datefmt: 日志日期格式    :return: None    """    logging.basicConfig(filename=filename, level=level, format=format, datefmt=datefmt)init_logger('apsched.log', logging.DEBUG, '%(asctime)s|%(levelname)s|%(message)s', '%Y-%m-%d %H:%M:%S')def job1():    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))def job2():    with open('hello.txt', 'a') as f:        f.write("hello, world!\n")"""BlockingScheduler 是 APScheduler 中的调度器,APScheduler 中有两种常用的调度器BlockingScheduler 和 BackgroundScheduler,当调度器是应用中唯一要运行的任务时,使用 BlockingSchedule,如果希望调度器在后台执行,使用 BackgroundScheduler。"""# 定义调度器类型:BlockingScheduler 前台阻塞调度器#sched = BlockingScheduler()sched = BackgroundScheduler()sched.add_job(job1, 'interval', seconds=5, max_instances=10)sched.add_job(job2, 'interval', seconds=1, max_instances=10)sched.start()sched.shutdown()


原创粉丝点击