python 定时任务-Advanced Python Scheduler

926 阅读1分钟

APScheduler特性:

- 支持一次、周期性的代码执行
- 支持任务存入数据库,重启是会执行未执行的。
- 具备跨平台。可以作为跨平台、特定于应用程序的、针对平台特定调度器的替换,例如cron守护进程或Windows任务调度器。

- APScheduler不是守护进程或服务本身,也没有任何命令行工具。
- 它主要用于在现有应用程序中运行。也就是说,APScheduler为构建调度器服务或运行专用调度器进程提供了一些构建块。

- APScheduler拥有三个内置的调度系统:
    cron式调度(具有可选的启动/结束时间)
    基于区间的执行(以均匀的间隔运行作业,可选的开始/结束时间)
    一次性延迟执行(在设定的日期/时间运行一次作业)
    
- 后端数据库:
    Memory
    SQLAlchemy (any RDBMS supported by SQLAlchemy works)
    MongoDB
    Redis
    RethinkDB
    ZooKeeper
- 也支持集成框架
    asyncio (PEP 3156)
    gevent
    Tornado
    Twisted
    Qt (using either PyQt or PySide)

使用

  • 安装

    • pip install apscheduler
    • python setup.py install ,从distribution 包中执行
  • 基础概念

    • triggers :

      • 触发器包含调度逻辑
    • job stores :

      • 存放job地方,通过序列化、反序列job 数据完成。
      • 默认是存放在内存中的
      • 调度器之间是不共享的。
    • executors :

      • 执行job
    • schedulers : 调度器

      • 提供接口,操作job、 job stores and executors
  • 选型

    • BlockingScheduler: use when the scheduler is the only thing running in your process
    • BackgroundScheduler: use when you’re not using any of the frameworks below, and want the scheduler to run in the background inside your application
    • AsyncIOScheduler: use if your application uses the asyncio module
    • GeventScheduler: use if your application uses gevent
    • TornadoScheduler: use if you’re building a Tornado application
    • TwistedScheduler: use if you’re building a Twisted application
    • QtScheduler: use if you’re building a Qt application
    • 待续。。