在作业库中找不到 Python 高级调度程序作业 (apscheduler)
考虑这一小段代码
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()
给了我正确的概述。
有人可以阐明这个问题吗?
Consider this small piece of code
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'
Why am I getting this error while printing out the jobs works?print_jobs()
gives me the right overview though.
Can someone shed some light on this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对大多数人来说可能是显而易见的,但我花了一段时间才明白。只是想分享一下我的代码的工作原理:
This might be obvious for most of the people, but it took me a while to get it. Wanted just to share what made my code work:
您必须将作业实例(由 add_interval_job 返回)传递给 unschedule_job 而不是字符串。
这解决了问题。
you must pass the job instance (returned by add_interval_job) to unschedule_job instead of a string.
That fixes the problem.
请注意,您的
First
类实际上拥有sched
实例;它当然无法访问您可能想要操作的Scheduler
的sched.sched
实例。也许您应该首先构造
Sched
对象,以便可以将其传递给First()
构造函数,以便可以调用它。我将勾画出一个未经测试的机制,我认为可以解决这个问题:这使
Sched
类保持独立 - 也许可以通过合并First< 来找到更清晰的设计/code> 和
Sched
—— 事实上,First
知道Sched
控制的作业名称,这表明有些事情不太对劲正确的。也许退一步解释一下你想要解决的问题是什么?这感觉不是最干净的解决方案,所以我想知道是否可以通过更好的机制来解决您遇到的问题。
Note that your
First
class doesn't actually have asched
instance; it certainly doesn't have access to thesched.sched
instance of theScheduler
that you probably want to manipulate.Perhaps you should construct the
Sched
object first, so you can pass it to theFirst()
constructor, so you can make calls to it. I'll sketch out an untested mechanism I think would solve this:This keeps the
Sched
class alone -- perhaps a cleaner design could be found by mergingFirst
andSched
-- the fact thatFirst
knows the name of a job thatSched
controls is a sign that something isn't quite right.Maybe take a step back and explain what problem you're trying to solve? This doesn't feel like the cleanest solution, so I have to wonder if the problem you've got could be solved through a better mechanism.