gpt4 book ai didi

python - APScheduler 作业重新启动后未运行

转载 作者:行者123 更新时间:2023-12-01 04:05:16 30 4
gpt4 key购买 nike

我尝试了APS的MongoJobStore example。示例代码为:

import logging
import os
import sys
from datetime import datetime, timedelta

from apscheduler.schedulers.blocking import BlockingScheduler

logging.basicConfig()


def alarm(time):
print('Alarm! This alarm was scheduled at %s.' % time)


if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_jobstore('mongodb', collection='example_jobs', host='192.168.0.108', port=27017)

if len(sys.argv) > 1 and sys.argv[1] == '--clear':
scheduler.remove_all_jobs()

alarm_time = datetime.now() + timedelta(seconds=10)
# scheduler.add_job(alarm, 'date', run_date=alarm_time, args=[datetime.now()], id="la")
print('To clear the alarms, run this example with the --clear argument.')
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

scheduler.start()

首先进行测试,我运行了代码,并且 alarm 方法的输出打印在控制台上。然后为了测试 mongo 的使用,我执行了以下操作:

  1. 运行示例并在执行作业之前停止它。
  2. 这在数据库中创建了以下条目

    > db.example_jobs.find().pretty()
    {
    "_id" : "la",
    "next_run_time" : 1456771825.792437,
    "job_state" : BinData(0,"gAJ9cQEoVQRhcmdzcQJjZGF0ZXRpbWUKZGF0ZXRpbWUKcQNVCgfgAwEAFA8MF4SFUnEEhXEFVQhleGVjdXRvcnEGVQdkZWZhdWx0cQdVDW1heF9pbnN0YW5jZXNxCEsBVQRmdW5jcQlVDl9fbWFpbl9fOmFsYXJtcQpVAmlkcQtVAmxhcQxVDW5leHRfcnVuX3RpbWVxDWgDVQoH4AMBABQZDBd1Y3B5dHoKX3AKcQ4oVQxBc2lhL0tvbGthdGFxD01YTUsAVQNJU1RxEHRScRGGUnESVQRuYW1lcRNVBWFsYXJtcRRVEm1pc2ZpcmVfZ3JhY2VfdGltZXEVSwFVB3RyaWdnZXJxFmNhcHNjaGVkdWxlci50cmlnZ2Vycy5kYXRlCkRhdGVUcmlnZ2VyCnEXKYFxGH1xGX1xGlUIcnVuX2RhdGVxG2gSc4ZiVQhjb2FsZXNjZXEciFUHdmVyc2lvbnEdSwFVBmt3YXJnc3EefXEfdS4=")
    }
  3. 然后我评论了添加作业的行:#scheduler.add_job(alarm, 'date', run_date=alarm_time, args=[datetime.now()], id="la")

  4. 然后我再次运行脚本并得到以下输出:

    To clear the alarms, run this example with the --clear argument.
    Press Ctrl+C to exit
    WARNING:apscheduler.executors.default:Run time of job "alarm (trigger: date[2016-03-01 00:35:27 IST], next run at: 2016-03-01 00:35:27 IST)" was missed by 0:00:19.290825
  5. 不打印alarm函数的输出,并且记录从数据库中删除。

我尝试调试第二次运行,代码正在从数据库读取作业,然后将其提交到线程池。但没有打印任何输出。

为什么会发生这种情况?

最佳答案

问题是 misfire_grace_time ( doc )。我没设置啊实际上,即使在示例本身中也没有设置。

工作代码:

import logging
import os
import sys
from datetime import datetime, timedelta

from apscheduler.schedulers.blocking import BlockingScheduler

logging.basicConfig()


def alarm(time):
print('Alarm! This alarm was scheduled at %s.' % time)


if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_jobstore('mongodb', collection='example_jobs', host='192.168.0.108', port=27017)

if len(sys.argv) > 1 and sys.argv[1] == '--clear':
scheduler.remove_all_jobs()

alarm_time = datetime.now() + timedelta(seconds=10)
scheduler.add_job(alarm, 'date', run_date=alarm_time, args=[datetime.now()], id="la", misfire_grace_time=1000)
print('To clear the alarms, run this example with the --clear argument.')
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

scheduler.start()

我从 APS 的 IRC channel #apscheduler 得到了这个答案。 @agronholm 帮助了我。

关于python - APScheduler 作业重新启动后未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35707630/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com