Python:为什么事件调度程序一直在漂移,有没有办法解决这个问题?

我想在 tkinter GUI 中使用调度程序作为秒表。我不确定这是否比使用后台线程更好,但我读到你不应该停止线程,而我想不断地启动和停止一个函数。


我在 Python 2.7 中做了一小段代码来测试调度程序,它似乎立即开始漂移。我希望它每秒增加一个计数器,但是一分钟后我就错了两秒(62 秒过去了,而不是 60 秒)。


这机器有关系吗?我的代码有问题吗?我应该使用另一个图书馆吗?


import sched, time


class Scheduler_Test:

    def __init__(self):

        self.counter = 0

        self.time_increment = 1.0


        self.end_time = 0.0


        self.s = sched.scheduler(time.time, time.sleep)


        self.start_time = time.time()

        self.s.enter(self.time_increment, 1, self.do_something, (self.s,))


        self.s.run() # run the event scheduler


    #Simple test of printing out the computer time (sec) and count

    def do_something(self, random_kwarg): 

        print "Time (sec):",time.time(),", count:", self.counter

        self.event = self.s.enter(self.time_increment, 1, self.do_something, (random_kwarg,))


        self.counter = self.counter + 1


Test = Scheduler_Test()


天涯尽头无女友
浏览 205回答 2
2回答

慕斯709654

如果您的目标是在长距离上跟上实际时间,请不要使用延迟。总是会有延迟,最终你会下班,这是原因——在事件开始和新事件调度之间有非零 CPU 工作,而且你总是有任务优先级。因此,如果您想要延迟 — 使用带有“run_after”接口的接口(sched以防万一.enter)。如果你想安排事情 - 使用“run_at”(.enterabs在你的情况下)。顺便说一句,考虑到您在 python 中只有一个进程,您仍然可以“迟到”,但这不是您可以影响的。旁注:您很少想重新定义调度程序计时器,默认值很好,它time.monotonic与回退一起使用time.time。如果您的代码能够在现实世界中使用,单调性将使您免于意外的痛苦。

冉冉说

用time.time实际做的时机。您可以使用 tkinter 的时钟更新用户界面,但将其更新为启动计时器时的时间增量值。我使用基于 tkinter 的 PySimpleGUI 制作了一个类似的应用程序。我的计时方式是这样的......伪代码如下,因为我正在使用包装器来执行这些功能......start_time = int(round(time.time() * 100))while True:    # use timer using tkinter's 'after' method    # tkroot.after(timeout, alarmcallback)  (Pseudocode)    # tkroot.mainloop()    #     in the alarm callback I called quit   tkroot.quit()    # Then I'm back in my code....    current_time = int(round(time.time() * 100)) - start_time    # Update the GUI with current_time
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python