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

发布于 2024-12-22 12:55:47 字数 1610 浏览 3 评论 0原文

考虑这一小段代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

胡渣熟男 2024-12-29 12:55:47

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

myJobName= "Homework"

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

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:

myJobName= "Homework"

for job in self.sched.get_jobs():
    if job.name == "Homework":
        self.sched.unschedule_job(job)
        print "No more homework!"
情愿 2024-12-29 12:55:47

您必须将作业实例(由 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.

聽兲甴掵 2024-12-29 12:55:47

请注意,您的 First 类实际上拥有 sched 实例;它当然无法访问您可能想要操作的 Schedulersched.sched 实例。

class First():
    def __init__(self):
        self.remove_job=None
    def go(self):
        self.remove_job('test')

也许您应该首先构造 Sched 对象,以便可以将其传递给 First() 构造函数,以便可以调用它。我将勾画出一个未经测试的机制,我认为可以解决这个问题:

class First():
    def __init__(self, sched):
        self.sched = sched
    def go(self):
        self.sched.remove_job('test')
def main():
    sched = Sched()
    first = First(sched)
    sched.sched.start()
    time.sleep(5)
    first.go()
    return 0

这使 Sched 类保持独立 - 也许可以通过合并 First< 来找到更清晰的设计/code> 和 Sched —— 事实上,First 知道 Sched 控制的作业名称,这表明有些事情不太对劲正确的。

也许退一步解释一下你想要解决的问题是什么?这感觉不是最干净的解决方案,所以我想知道是否可以通过更好的机制来解决您遇到的问题。

Note that your First class doesn't actually have a sched instance; it certainly doesn't have access to the sched.sched instance of the Scheduler that you probably want to manipulate.

class First():
    def __init__(self):
        self.remove_job=None
    def go(self):
        self.remove_job('test')

Perhaps you should construct the Sched object first, so you can pass it to the First() constructor, so you can make calls to it. I'll sketch out an untested mechanism I think would solve this:

class First():
    def __init__(self, sched):
        self.sched = sched
    def go(self):
        self.sched.remove_job('test')
def main():
    sched = Sched()
    first = First(sched)
    sched.sched.start()
    time.sleep(5)
    first.go()
    return 0

This keeps the Sched class alone -- perhaps a cleaner design could be found by merging First and Sched -- the fact that First knows the name of a job that Sched 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文