博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python定时任务框架apscheduler,定时执行多个固定任务
阅读量:4199 次
发布时间:2019-05-26

本文共 2108 字,大约阅读时间需要 7 分钟。

apscheduler

Python 的 apscheduler 提供了非常丰富而且方便易用的定时任务接口。

apscheduler使用起来十分方便。提供了基于日期、固定时间间隔以及crontab 类型的任务,我们可以在主程序的运行过程中快速增加新作业或删除旧作业,如果把作业存储在数据库中,那么作业的状态会被保存,当调度器重启时,不必重新添加作业,作业会恢复原状态继续执行。apscheduler 可以当作一个跨平台的调度工具来使用,可以做为 linux 系统crontab 工具或 windows 计划任务程序的替换。注意,apscheduler 不是一个守护进程或服务,它自身不带有任何命令行工具。它主要是要在现有的应用程序中运行,也就是说,apscheduler 为我们提供了构建专用调度器或调度服务的基础模块。

安装

pin install apscheduler

触发器(triggers)

触发器包含调度逻辑,描述一个任务何时被触发,按日期或按时间间隔或按 cronjob 表达式三种方式触发。每个作业都有它自己的触发器,除了初始配置之外,触发器是完全无状态的。

作业存储器(job stores)

作业存储器指定了作业被存放的位置,默认情况下作业保存在内存,也可将作业保存在各种数据库中,当作业被存放在数据库中时,它会被序列化,当被重新加载时会反序列化。作业存储器充当保存、加载、更新和查找作业的中间商。在调度器之间不能共享作业存储。

执行器(executors)

执行器是将指定的作业(调用函数)提交到线程池或进程池中运行,当任务完成时,执行器通知调度器触发相应的事件。

调度器(schedulers)

任务调度器,属于控制角色,通过它配置作业存储器、执行器和触发器,添加、修改和删除任务。调度器协调触发器、作业存储器、执行器的运行,通常只有一个调度程序运行在应用程序中,开发人员通常不需要直接处理作业存储器、执行器或触发器,配置作业存储器和执行器是通过调度器来完成的。

间隔性任务

# -*- coding: utf-8 -*-import osfrom datetime import datetimefrom apscheduler.schedulers.blocking import BlockingSchedulerdef tick():    print('Tick! The time is: %s' % datetime.now())if __name__ == '__main__':    scheduler = BlockingScheduler()    scheduler.add_job(tick, 'interval', seconds=3)    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C    '))    try:        scheduler.start()    except (KeyboardInterrupt, SystemExit):        pass

每间隔固定时间执行一次任务,执行结果:

Press Ctrl+C     to exitTick! The time is: 2020-03-31 10:02:09.366383Tick! The time is: 2020-03-31 10:02:12.369104Tick! The time is: 2020-03-31 10:02:15.368425Tick! The time is: 2020-03-31 10:02:18.370386

cron 任务

# -*- coding: utf-8 -*-import osfrom datetime import datetimefrom apscheduler.schedulers.blocking import BlockingSchedulerdef tick():    print('Tick! The time is: %s' % datetime.now())if __name__ == '__main__':    scheduler = BlockingScheduler()    scheduler.add_job(tick, 'cron', hour=9, minute=52)    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C    '))    try:        scheduler.start()    except (KeyboardInterrupt, SystemExit):        pass

在固定的时间执行任务,执行结果:

Press Ctrl+C     to exitTick! The time is: 2020-03-31 09:52:00.003977

转载地址:http://fqfli.baihongyu.com/

你可能感兴趣的文章
【一天一道LeetCode】#83. Remove Duplicates from Sorted List
查看>>
【一天一道LeetCode】#91. Decode Ways
查看>>
【一天一道LeetCode】#92. Reverse Linked List II
查看>>
【一天一道LeetCode】#93. Restore IP Addresses
查看>>
【一天一道LeetCode】#94. Binary Tree Inorder Traversal
查看>>
【一天一道LeetCode】#112. Path Sum
查看>>
【一天一道LeetCode】#113. Path Sum II
查看>>
【一天一道LeetCode】#114. Flatten Binary Tree to Linked List
查看>>
【unix网络编程第三版】阅读笔记(二):套接字编程简介
查看>>
【一天一道LeetCode】#115. Distinct Subsequences
查看>>
【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node
查看>>
【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
查看>>
【一天一道LeetCode】#118. Pascal's Triangle
查看>>
【一天一道LeetCode】#119. Pascal's Triangle II
查看>>
【unix网络编程第三版】ubuntu端口占用问题
查看>>
【一天一道LeetCode】#120. Triangle
查看>>
【unix网络编程第三版】阅读笔记(三):基本套接字编程
查看>>
【一天一道LeetCode】#121. Best Time to Buy and Sell Stock
查看>>
【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
查看>>
【一天一道LeetCode】#125. Valid Palindrome
查看>>