apscheduler [任务调度]

    # 安装   pip install apscheduler
    # 例子   https://bitbucket.org/agronholm/apscheduler/src/e6298f953a68/tests/?at=master

    scheduler.start()                                                   # 启动任务
    job = scheduler.add_job(myfunc, 'interval', minutes=2)              # 添加任务
    job.remove()                                                        # 删除任务
    scheduler.add_job(myfunc, 'interval', minutes=2, id='my_job_id')    # 添加任务
    scheduler.remove_job('my_job_id')                                   # 删除任务
    job.modify(max_instances=6, name='Alternate name')                  # 修改工作
    scheduler.shutdown()                                                # 关闭调度
    scheduler.shutdown(wait=False)                                      # 关闭调度  不等待
    # 暂停
    apscheduler.job.Job.pause()
    apscheduler.schedulers.base.BaseScheduler.pause_job()
    # 恢复
    apscheduler.job.Job.resume()
    apscheduler.schedulers.base.BaseScheduler.resume_job()

定时任务

        from pytz import utc
        from apscheduler.schedulers.background import BackgroundScheduler
        from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor
        import time

        executors = {
            'default': ThreadPoolExecutor(20),
            'processpool': ProcessPoolExecutor(5)
        }
        job_defaults = {
            'coalesce': False,
            'max_instances': 3
        }
        scheduler = BackgroundScheduler( executors=executors, job_defaults=job_defaults, timezone=utc)

        def myfunc():
            print 'test'

        scheduler.add_job(myfunc, 'interval', minutes=1, id='myworkid')
        scheduler.start()

        try:
            while True:
                time.sleep(2)
                # add_job
        except (KeyboardInterrupt, SystemExit):
            scheduler.shutdown()
文档更新时间: 2018-11-21 17:35   作者:RuM