APScheduler -调度器 BlockingScheduler

schedulers 调度器主要分三种,一种独立运行的,一种是后台运行的,最后一种是配合其它程序使用

  • BlockingScheduler: 当这个调度器是你应用中 唯一要运行 的东西时使用
  • BackgroundScheduler: 当 不运行其它框架 的时候使用,并使你的任务在 后台运行
  • AsyncIOScheduler: 当你的程序是 异步IO模型 的时候使用

BlockingScheduler

当你的应用中,仅仅只运行定时任务,其它代码都不执行的时候,可以用到BlockingScheduler 调度器。比如你写了一个简单的程序,设置一个定时任务去抓取页面的数据。那就可以用到BlockingScheduler.
或者你单独去调试看定时任务有没执行

from apscheduler.schedulers.blocking import BlockingScheduler  
import time  
  
  
# 仅运行定时任务  
scheduler = BlockingScheduler()  
  
  
# 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()

运行结果

task 2 executed  yyy-------- 1698211090.014796
task 2 executed  yyy-------- 1698211095.0198605
task 1 executed  xxxx-------- 1698211097.7044744
task 2 executed  yyy-------- 1698211100.0056248
task 2 executed  yyy-------- 1698211105.0121682
task 1 executed  xxxx-------- 1698211107.6990259
task 2 executed  yyy-------- 1698211110.0029516
task 2 executed  yyy-------- 1698211115.011106
task 1 executed  xxxx-------- 1698211117.699221

scheduler.start()

scheduler.start() 调用会阻塞主线程

# 添加一个定时任务  
scheduler.add_job(  
    task2, 'cron', second='*/5',  
    args=["yyy"], id="task_2", replace_existing=True  
)  
  
scheduler.start()  
  
while(True):  
    print('main ---------------')  
    time.sleep(1)

如果后面还有其它代码,会一直没法执行。

如果有其它代码需要执行,可以使用BackgroundScheduler,使你的任务在 后台运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,对于django-apscheduler,可以使用多个调度和作业存储。下面是一个示例: ```python # settings.py SCHEDULER_JOBSTORES = { 'default': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite') } SCHEDULER_EXECUTORS = { 'default': ThreadPoolExecutor(20), 'processpool': ProcessPoolExecutor(5) } SCHEDULER_JOB_DEFAULTS = { 'coalesce': False, 'max_instances': 3 } SCHEDULER_API_ENABLED = True ``` 在这个例子中,我们定义了一个默认的作业存储,并使用了 SQLAlchemyJobStore。我们还定义了两个执行:一个默认的线程池执行,和一个进程池执行。最后,我们还启用了调度的 API。 在您的应用程序中,您可以创建多个调度并指定它们的作业存储和执行。例如: ```python # views.py from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers.blocking import BlockingScheduler scheduler1 = BackgroundScheduler(jobstores={'default': SQLAlchemyJobStore(url='sqlite:///scheduler1_jobs.sqlite')}, executors={'default': ThreadPoolExecutor(10)}, job_defaults={'coalesce': False, 'max_instances': 3}) scheduler2 = BlockingScheduler(jobstores={'default': SQLAlchemyJobStore(url='sqlite:///scheduler2_jobs.sqlite')}, executors={'default': ProcessPoolExecutor(5)}, job_defaults={'coalesce': False, 'max_instances': 1}) ``` 在这个例子中,我们创建了两个调度scheduler1 和 scheduler2。它们分别使用不同的作业存储和执行,并提供了不同的作业默认值。 希望这个示例能帮助您理解如何在django-apscheduler中使用多个调度和作业存储。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值