【问题标题】:Python Apscheduler is not shutting downPython Apscheduler 没有关闭
【发布时间】:2016-01-01 13:16:46
【问题描述】:

我们需要在当前作业执行时动态调度多个作业。

大概的场景是:

应该有一个调度程序来每天检查应用程序表(假设在 6 AM UTC)。 查找今天日期时间为 resume_dttime 的用户 为该用户动态安排工作并在今天的 resume_dttime 启动他的服务 所以我的代码是:

from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()

def scheduled_job():
    """
    """
    liveusers = todays_userslist() #Get users from table with todays resume_dttime
    for u in liveusers:
        user_job = get_userjob(u.id)
        runtime = u.resume_dttime #eg u.resume_dttime is datetime(2015, 12, 13, 16, 30, 5)
        sched.add_job(user_job, 'date', run_date=runtime, args=[u.name])


if __name__ == "__main__":
  scheduled_job()
  sched.start()
  sched.shutdown(wait=True)

代码运行良好(对于一项工作)。没有检查多个工作,但它根本没有停止。我尝试删除 for 循环。此外,在google 上进行了许多查询。但没有运气:( 要退出,每次都需要通过CTrl+C退出

【问题讨论】:

  • 代码块在哪里?在运行调度程序期间还是之前?你能发送你的 CTRL-C 的回溯吗?
  • 我不明白为什么阻止不是预期的行为。调度程序的作用是在后台运行并偶尔启动一个作业,我错过了什么吗? cron 对我来说是后台运行的唯一替代方案。

标签: python shutdown apscheduler


【解决方案1】:

正如documentation 所说,它在 start() 处阻塞。 start() 调用只有在调度程序从另一个线程关闭时才会返回,而您在任何地方都没有这样做(从上面的 sn-p 判断)。

【讨论】:

  • 好的,谢谢,知道了!所以它永远不会出现在sched.shutdown(wait=True)。你能告诉我一个通过另一个线程关闭它的例子吗?
  • 可能从您的一项预定工作中关闭它?只需在此处调用 sched.shutdown() 即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多