gpt4 book ai didi

Python APScheduler 删除作业后抛出异常

转载 作者:太空宇宙 更新时间:2023-11-03 20:22:23 75 4
gpt4 key购买 nike

我正在 redis 中添加作业,完成作业后我添加了一个事件处理程序。在事件处理程序中,我返回值,根据该值我从作业库中删除作业 ID。它被成功删除,但立即抛出异常。

代码

from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.events import EVENT_JOB_EXECUTED
import logging
logging.basicConfig()

scheduler = BackgroundScheduler()
scheduler.add_jobstore('redis')
scheduler.start()

def tick():
print('Tick! The time is: %s' % datetime.now())
return 'success'

def removing_jobs(event):
if event.retval == 'success':
scheduler.remove_job(event.job_id)

scheduler.add_listener(removing_jobs, EVENT_JOB_EXECUTED)

try:
count = 0
while True:
count += 1
time.sleep(10)
job_ret = scheduler.add_job(tick, 'interval', id = str(count), seconds=10)
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()

异常

Exception in thread APScheduler:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/.virtualenvs/py3/lib/python3.5/site-packages/apscheduler/schedulers/blocking.py", line 30, in _main_loop
wait_seconds = self._process_jobs()
File "/.virtualenvs/py3/lib/python3.5/site-packages/apscheduler/schedulers/base.py", line 995, in _process_jobs
jobstore.update_job(job)
File "/.virtualenvs/py3/lib/python3.5/site-packages/apscheduler/jobstores/redis.py", line 91, in update_job
raise JobLookupError(job.id)
apscheduler.jobstores.base.JobLookupError: 'No job by the id of 1 was found'

最佳答案

简而言之:您正在处理正在处理的作业;因此您应该在执行之外删除该作业。

这是因为调度程序不知道作业的执行会做什么;因此它启动 tick 并将一个 job 对象发送到 redis jobstore 认为它将再次执行。在此之前,EVENT_JOB_LISTENER 会启动 removing_jobs

问题是,当redis'jobstore获取作业以更新其状态时,它已经被删除,因此会引发JobLookupError .

关于Python APScheduler 删除作业后抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58073493/

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