APScheduler-调度器 BackgroundScheduler

当你有主程序需要执行,让定时任务在后台执行时,可以用BackgroundScheduler

python 复制代码
from apscheduler.schedulers.background import BackgroundScheduler  
import time  
  
  
# 仅运行定时任务  
scheduler = BackgroundScheduler()  
  
  
# interval example, 间隔执行, 每10秒执行一次  
def task1(x):  
    print(f'task 1 executed  {x}--------', time.time())  
  
  
# 添加一个定时任务  
scheduler.add_job(  
    task1, 'interval', seconds=10,  
    args=["xxxx"], id="task_1", replace_existing=True  
)  
  
  
# cron examples, 每5秒执行一次 相当于interval 间隔调度中seconds = 5  
def task2(y):  
    print(f'task 2 executed  {y}--------', time.time())  
  
  
# 添加一个定时任务  
scheduler.add_job(  
    task2, 'cron', second='*/5',  
    args=["yyy"], id="task_2", replace_existing=True  
)  
  
scheduler.start()  
  
while(True):  
    print('main ---------------')  
    time.sleep(1)

执行结果

复制代码
main ---------------
main ---------------
main ---------------
main ---------------
task 2 executed  yyy-------- 1698211520.0085256
main ---------------
main ---------------

Flask-APScheduler 中默认使用的就是 BackgroundScheduler

相关推荐
言乐61 小时前
Python实现建造微服务商城后台
开发语言·python·算法·微服务·架构
fenglllle1 小时前
chromadb emmbedding 向量检索
人工智能·python·embedding
cui_ruicheng1 小时前
Python从入门到实战(六):非序列容器
开发语言·python
飞猪~2 小时前
Langchain python版本 LLM 重要函数invoke,ainvoke,stream,astream,batch,abatch
python·langchain·batch
沙蒿同学2 小时前
当古诗词遇上 AI:从 38 万句诗词中取一个好名字
python·算法·架构
xxie1237943 小时前
Python装饰器与语法糖
开发语言·python
CClaris3 小时前
大模型量化从0到1(五):GPTQ 原理详解 + 从零量化一个真实大模型
人工智能·python·算法·机器学习
Railshiqian3 小时前
UserPickerActivity 内部逻辑分析
开发语言·python
一tiao咸鱼3 小时前
前端转 agent # 02 - FastAPI 框架入门与原理
前端·python
Yolo566Q4 小时前
Noah-MP陆面过程模型建模方法与站点、区域模拟实践技术应用
开发语言·python