【问题标题】:APScheduler run a job for a specific amount of timesAPScheduler 运行作业特定次数
【发布时间】:2017-07-01 00:50:46
【问题描述】:

Python 3.6.0 APScheduler 3.3.1

我刚刚修改了一个程序来实现 APScheduler。

程序完全按预期运行。

这是我在APScheduler中添加的代码sn-p:

def main():
    sched = BackgroundScheduler()
    sched.start()
    sched.add_job(errordetect)
    sched.add_job(errordetect, 'interval', minutes=5)
    input("Press enter to exit.")

作业“立即”运行,然后每 5 分钟运行一次。我的问题是这样的: 我必须按“Enter”手动停止作业。

有没有办法说: 1)在X时间(比如一小时)后停止运行或 2) 在 X 次迭代后停止(比如 12 次迭代)

我想要的是每 5 分钟运行一次作业一小时,所以我 希望它在十二次迭代(或一小时——实际上是同一件事)后自行停止。我在文档中没有找到类似的东西,但我希望我在文档中遗漏了一些东西,或者有另一种方法来完成我所追求的。

谢谢。

【问题讨论】:

    标签: python-3.6 apscheduler


    【解决方案1】:

    我是这样解决的:

    x = 0
    starttime = time.time()
    while x < 5:
        i = 0
        for a_connection in ssh_conns:
            errordetect(a_connection, ip_addrs[i])
            i += 1
        x += 1
        if x == 5:
            break
        time.sleep(15.0 - ((time.time() - starttime) % 15.0))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 2015-11-28
      • 1970-01-01
      相关资源
      最近更新 更多