Python 线程定时器问题

4

我一直在尝试使用Python创建计时器程序时遇到问题。 我希望用户可以输入计时器倒计时的时间,并且每隔0.1秒更新一次。 到目前为止,我有以下代码:

from gi.repository import Gtk
import time
import threading

class TimerWindow(Gtk.Window):
  def __init__(self):
    Gtk.Window.__init__(self, title = "Timer")

    self.box = Gtk.Box(spacing = 1)
    self.add(self.box)

    self.entry = Gtk.Entry()
    self.entry.connect("activate", self.start, self.entry)
    self.box.pack_start(self.entry, True, True, 0)

  def start(self, widget, entry):
    starttime = time.time()
    totaltime = float(entry.get_text())
    self.update(starttime, totaltime, entry)

  def update(self, starttime, totaltime, entry):
    entry.set_text(str(totaltime - (time.time() - starttime)))
    if float(entry.get_text()) > 0:
      t = threading.Timer(0.1, self.update, [starttime, totaltime, entry])
      t.start()

win = TimerWindow()
win.connect("delete-event", Gtk.main_quit)
win.set_keep_above(True)
win.show_all()
Gtk.main()

这似乎在一段时间内有点起作用,但有时会返回以下内容:

timer.py:31: Warning: g_object_ref: assertion 'object->ref_count > 0' failed
  Gtk.main()
Segmentation fault

我不知道是什么原因导致这种情况发生,需要一些帮助。能否有人帮我找到停止这种情况发生的方法?

1个回答

2
你的程序崩溃是因为它从不同线程调用GTK API,这是禁止的。幸运的是,修改以使其正确工作非常容易 - 你可以使用线程,只需要确保所有GTK调用都来自GUI线程,即运行主循环的线程。最简单的方法是让你的工作线程不直接执行GUI调用,而是使用GObject.idle_add将其分派到主线程。因此,不要从计时器中调用self.update,而是调用一个新方法schedule_update,它会安排实际的update从GUI线程调用。由于GUI线程没有被阻塞,更新实际上会立即运行:
  def update(self, starttime, totaltime, entry):
    entry.set_text(str(totaltime - (time.time() - starttime)))
    if float(entry.get_text()) > 0:
      t = threading.Timer(0.1, self.schedule_update, [starttime, totaltime, entry])
      t.start()

  def schedule_update(self, *args):
      GObject.idle_add(self.update, *args)

(当然,你还需要从gi.repository中导入GObject。)
毋庸置疑,这种调度最好使用GTK主循环超时来实现(参见GObject.timeout_add),因为它们在首次执行回调时就在GUI线程内部执行,不需要任何额外的调度。但是,在某些合理的情况下,使用线程是适当的,例如调用长时间运行的同步API(如数据库访问)而不会冻结GUI,或执行内部释放GIL的计算。

GObject.timeout_add 的方式运行良好。当使用线程时,即使添加了 schedule_update,我仍然会遇到与之前相同的错误。也许我做错了什么,但毕竟,我对 Python 线程和相关内容还很陌生,所以随便了。 - K Zhang
等等,算了。我太蠢了,没有看到线程启动应该运行schedule_update而不是update。这个答案是100%正确的。 - K Zhang
如果 GObject.timeout_add 能够满足您的需求,那么请忘记线程(就此目的而言),因为它们会引入许多复杂性和问题。一旦您在 Python 和 GTK 方面获得更多经验,您将能够在必要时安全地使用线程。 - user4815162342

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接