详解python调度框架APScheduler使用
发布时间 - 2026-01-11 00:23:58 点击率:次最近在研究python调度框架APScheduler使用的路上,那么今天也算个学习笔记吧!
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import time
import os
from apscheduler.schedulers.background import BackgroundScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = BackgroundScheduler()
scheduler.add_job(tick, 'interval', seconds=3) #间隔3秒钟执行一次
scheduler.start() #这里的调度任务是独立的一个线程
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2) #其他任务是独立的线程执行
print('sleep!')
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')
非阻塞调度,在指定的时间执行一次
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import time
import os
from apscheduler.schedulers.background import BackgroundScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = BackgroundScheduler()
#scheduler.add_job(tick, 'interval', seconds=3)
scheduler.add_job(tick, 'date', run_date='2016-02-14 15:01:05') #在指定的时间,只执行一次
scheduler.start() #这里的调度任务是独立的一个线程
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2) #其他任务是独立的线程执行
print('sleep!')
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')
非阻塞的方式,采用cron的方式执行
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import time
import os
from apscheduler.schedulers.background import BackgroundScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = BackgroundScheduler()
#scheduler.add_job(tick, 'interval', seconds=3)
#scheduler.add_job(tick, 'date', run_date='2016-02-14 15:01:05')
scheduler.add_job(tick, 'cron', day_of_week='6', second='*/5')
'''
year (int|str) – 4-digit year
month (int|str) – month (1-12)
day (int|str) – day of the (1-31)
week (int|str) – ISO week (1-53)
day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
hour (int|str) – hour (0-23)
minute (int|str) – minute (0-59)
second (int|str) – second (0-59)
start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)
end_date (datetime|str) – latest possible date/time to trigger on (inclusive)
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)
* any Fire on every value
*/a any Fire every a values, starting from the minimum
a-b any Fire on any value within the a-b range (a must be smaller than b)
a-b/c any Fire every c values within the a-b range
xth y day Fire on the x -th occurrence of weekday y within the month
last x day Fire on the last occurrence of weekday x within the month
last day Fire on the last day within the month
x,y,z any Fire on any matching expression; can combine any number of any of the above expressions
'''
scheduler.start() #这里的调度任务是独立的一个线程
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2) #其他任务是独立的线程执行
print('sleep!')
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')
阻塞的方式,间隔3秒执行一次
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import os
from apscheduler.schedulers.blocking import BlockingScheduler
def 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):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')
采用阻塞的方法,只执行一次
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import os
from apscheduler.schedulers.blocking import BlockingScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(tick, 'date', run_date='2016-02-14 15:23:05')
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
scheduler.start() #采用的是阻塞的方式,只有一个线程专职做调度的任务
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')
采用阻塞的方式,使用cron的调度方法
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import os
from apscheduler.schedulers.blocking import BlockingScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(tick, 'cron', day_of_week='6', second='*/5')
'''
year (int|str) – 4-digit year
month (int|str) – month (1-12)
day (int|str) – day of the (1-31)
week (int|str) – ISO week (1-53)
day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
hour (int|str) – hour (0-23)
minute (int|str) – minute (0-59)
second (int|str) – second (0-59)
start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)
end_date (datetime|str) – latest possible date/time to trigger on (inclusive)
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)
* any Fire on every value
*/a any Fire every a values, starting from the minimum
a-b any Fire on any value within the a-b range (a must be smaller than b)
a-b/c any Fire every c values within the a-b range
xth y day Fire on the x -th occurrence of weekday y within the month
last x day Fire on the last occurrence of weekday x within the month
last day Fire on the last day within the month
x,y,z any Fire on any matching expression; can combine any number of any of the above expressions
'''
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
scheduler.start() #采用的是阻塞的方式,只有一个线程专职做调度的任务
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# python
# 调度框架
# apscheduler
# python3
# Python定时库Apscheduler的简单使用
# Python中定时任务框架APScheduler的快速入门指南
# Python定时任务APScheduler的实例实例详解
# Python定时任务工具之APScheduler使用方式
# Python任务调度利器之APScheduler详解
# Python APScheduler执行使用方法详解
# Python定时库APScheduler的原理以及用法示例
# 的是
# 只有一个
# 也算
# 大家多多
# 学习笔记
# 路上
# Break
# exit
# format
# simulate
# application
# nt
# Ctrl
# interval
# add_job
# Press
# start
# seconds
# activity
# mode
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel storage目录权限问题_Laravel文件写入权限设置
怎么用AI帮你设计一套个性化的手机App图标?
制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?
高端网站建设与定制开发一站式解决方案 中企动力
laravel怎么为API路由添加签名中间件保护_laravel API路由签名中间件保护方法
网站建设要注意的标准 促进网站用户好感度!
Bootstrap整体框架之CSS12栅格系统
,网页ppt怎么弄成自己的ppt?
Laravel如何实现用户注册和登录?(Auth脚手架指南)
打造顶配客厅影院,这份100寸电视推荐名单请查收
ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】
如何快速生成高效建站系统源代码?
如何快速搭建高效简练网站?
北京网页设计制作网站有哪些,继续教育自动播放怎么设置?
JS经典正则表达式笔试题汇总
ChatGPT 4.0官网入口地址 ChatGPT在线体验官网
为什么php本地部署后css不生效_静态资源加载失败修复技巧【技巧】
Laravel项目怎么部署到Linux_Laravel Nginx配置详解
如何在阿里云香港服务器快速搭建网站?
移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?
lovemo网页版地址 lovemo官网手机登录
laravel怎么配置Redis作为缓存驱动_laravel Redis缓存配置教程
公司门户网站制作公司有哪些,怎样使用wordpress制作一个企业网站?
原生JS实现图片轮播切换效果
韩国服务器如何优化跨境访问实现高效连接?
如何在景安服务器上快速搭建个人网站?
创业网站制作流程,创业网站可靠吗?
香港服务器建站指南:免备案优势与SEO优化技巧全解析
如何快速查询网站的真实建站时间?
UC浏览器如何切换小说阅读源_UC浏览器阅读源切换【方法】
Linux网络带宽限制_tc配置实践解析【教程】
浅析上传头像示例及其注意事项
JavaScript常见的五种数组去重的方式
如何解决hover在ie6中的兼容性问题
如何在建站主机中优化服务器配置?
,南京靠谱的征婚网站?
详解Oracle修改字段类型方法总结
网站图片在线制作软件,怎么在图片上做链接?
EditPlus中的正则表达式 实战(2)
Firefox Developer Edition开发者版本入口
Laravel怎么进行数据库回滚_Laravel Migration数据库版本控制与回滚操作
安克发布新款氮化镓充电宝:体积缩小 30%,支持 200W 输出
Windows11怎样设置电源计划_Windows11电源计划调整攻略【指南】
深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?
1688铺货到淘宝怎么操作 1688一键铺货到自己店铺详细步骤
Laravel怎么连接多个数据库_Laravel多数据库连接配置
Laravel如何使用Service Provider服务提供者_Laravel依赖注入与容器绑定【深度】
原生JS获取元素集合的子元素宽度实例
悟空浏览器如何设置小说背景色_悟空浏览器背景色设置【方法】
php8.4header发送头信息失败怎么办_php8.4header函数问题解决【解答】

