python apscheduler不一致 [英] python apscheduler not consistent

查看:184
本文介绍了python apscheduler不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 web.py 框架内使用 python apscheduler 运行调度程序. 函数 runserver 应该每天早上9点运行,但不一致. 它运行大多数天,但有时会跳过一天.

I'm running a scheduler using python apscheduler inside web.py framework. The function runserver is supposed to run everyday at 9 a.m but it is inconsistent. It runs most days but skips a day once in a while.

代码:

import web
from apscheduler.schedulers.blocking import BlockingScheduler #Blocking Scheduler

#URLs
urls = (
    '/startscheduler/','index',
    )

Nightlysched = BlockingScheduler()

@Nightlysched.scheduled_job('cron', hour=9)
def runserver():
    print 2+2 #doing some calculations here

#Main function to run the cron JOB
if __name__ == "__main__":
    Nightlysched.start() #stating the job
    app = web.application(urls, globals())
    app.run() 

将调度程序配置为每天上午9点运行的正确方法是什么?

What is the correct way to configure the scheduler to run every day at 9.a.m?

推荐答案

APScheduler具有宽限期,在此期间允许运行作业.如果由于某种原因,调度程序很忙和/或主机的负载太高,则APScheduler可能无法及时启动作业.

APScheduler has a grace period during which jobs are allowed to run. If for some reason the scheduler is busy and/or the load of the host is too high, APScheduler might fail to start the job in time.

在这种情况下,如果无法在宽限期内启动该作业,则该作业将被丢弃(如果您已初始化Python日志记录,则会记录一条说明性消息).

In this cases, the job will be discarded if it could not be started during the grace time (an explanatory message will be logged if you have initialized Python logging).

取决于实际的根本原因:

Depending on the actual root cause:

  • 如果调度程序无法及时调度作业,则可以使用misfire_grace_time=None告诉APScheduler尽快调度作业,而不是将其丢弃.
  • 默认情况下,每个作业只能同时运行一个实例.确保上一次运行已完成.通过在添加作业时使用max_instances关键字参数,可以为调度程序允许并发运行的特定作业设置最大实例数.在这种情况下,您可能还需要使用coalesce=False.仅当作业花费的时间超过24小时(对于您而言),并且您接受作业的两个实例可以同时运行时,才执行此操作.
  • 如果正在运行的作业太多,但是计算机负载不是很高,则意味着您的作业数量超过了可以同时运行的数量.您可以尝试增加APScheduler executor 用于运行作业的线程池的大小(这取决于您的设置,请检查:
  • If the scheduler failed to schedule the job in time, you can use misfire_grace_time=None to tell APScheduler to schedule the job as soon as it can instead of discarding it.
  • By default, only one instance of each job is allowed to be run at the same time. Make sure the previous run has finished. It is possible to set the maximum number of instances for a particular job that the scheduler will let run concurrently, by using the max_instances keyword argument when adding the job. In this case you also may need to use coalesce=False. Do this only if the job takes longer than 24 hours (in your case) and you accept that two instances of your job could be running simultaneously.
  • If there were too many jobs running, but the machine load wasn't too high, it means you have more jobs than what you can run concurrently. You can try increasing the size of the thread pool that APScheduler executor is using to run jobs (this depends on your setup, check: http://apscheduler.readthedocs.org/en/latest/userguide.html).

总而言之,我首先尝试使用misfire_grace_period:

In summary, I'd first try with misfire_grace_period:

@Nightlysched.scheduled_job('cron', hour=9, misfire_grace_time=None)

但是,请注意,正如@Alex所提到的,我不理解您的代码为何起作用,因为对Nightlysched.start()的调用应该阻止并阻止您的Web应用程序运行.我想这是粘贴的代码,并不能真正代表您正在运行的代码.对我来说,您似乎应该改用像BackgroundScheduler这样的非阻塞调度程序.

As a note, though, as @Alex mentioned I don't grasp why your code works, because the call to Nightlysched.start() should be blocking and preventing your web application from running. I guess this is pasted code and doesn't really represent what you are running. To me, it looks like you should instead be using a non-blocking scheduler like BackgroundScheduler.

这篇关于python apscheduler不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆