If you are using the new Python GObject Introspection API , you should use GLib.timeout_add() .
Please note that the documentation seems to be incorrect. This is actually:
timeout_add (interval, function, * user_data, ** kwargs)
Here is an example. Note that run is a callable object, but it can be any regular function or method.
from gi.repository import GLib class Runner: def __init__(self, num_times): self.num_times = num_times self.count = 0 def __call__(self, *args): self.count += 1 print("Periodic timer [{}]: args={}".format(self.count, args)) return self.count < self.num_times run = Runner(5) interval_ms = 1000 GLib.timeout_add(interval_ms, run, 'foo', 123) loop = GLib.MainLoop() loop.run()
Conclusion:
$ python3 glib_timeout.py Periodic timer [1]: args=('foo', 123) Periodic timer [2]: args=('foo', 123) Periodic timer [3]: args=('foo', 123) Periodic timer [4]: args=('foo', 123) Periodic timer [5]: args=('foo', 123) <messages stop but main loop keeps running>
source share