gpt4 book ai didi

在 jobstore (apscheduler) 中找不到 Python 高级调度程序作业

转载 作者:太空宇宙 更新时间:2023-11-04 10:57:34 25 4
gpt4 key购买 nike

考虑这段小代码

from apscheduler.scheduler import Scheduler
import time

class First():
def __init__(self):
self.remove_job=None
def go(self):
self.remove_job('test')
class Sched():
def __init__(self):
self.sched = Scheduler()
self.sched.add_interval_job( self.execute,
seconds=1,
name = 'test'
)
def execute(self):
print "i'm alive"
def remove_job(self,job):
self.sched.print_jobs()
self.sched.unschedule_job(job)

def main():
first = First()
sched = Sched()
first.remove_job=sched.remove_job
sched.sched.start()
time.sleep(5)
first.go()
return 0

if __name__ == '__main__':
main()

python sched_test.py
i'm alive
i'm alive
i'm alive
i'm alive
i'm alive
Jobstore default:
test (trigger: interval[0:00:01], next run at: 2011-12-22 01:25:36.577572)
Traceback (most recent call last):
File "sched_test.py", line 55, in <module>
main()
File "sched_test.py", line 51, in main
first.go()
File "sched_test.py", line 31, in go
self.remove_job('test')
File "sched_test.py", line 43, in remove_job
self.sched.unschedule_job(job)
File "/usr/local/lib/python2.7/dist-packages/APScheduler-2.0.2-py2.7.egg/apscheduler/scheduler.py", line 401, in unschedule_job
raise KeyError('Job "%s" is not scheduled in any job store' % job)
KeyError: 'Job "test" is not scheduled in any job store'

为什么在打印作业作品时出现此错误?print_jobs() 给了我正确的概述。

有人可以阐明这个问题吗?

最佳答案

这对大多数人来说可能是显而易见的,但我花了一段时间才明白。只想分享是什么让我的代码工作:

myJobName= "Homework"

for job in self.sched.get_jobs():
if job.name == "Homework":
self.sched.unschedule_job(job)
print "No more homework!"

关于在 jobstore (apscheduler) 中找不到 Python 高级调度程序作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8598165/

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