I have converted some Python code to use PYGI but I can't figure out how to convert this line:

gtk.timeout_add(PING_FREQUENCY * 1000, self.doWork)

To by clear, since I don't really know the difference between PYGTK and PYGI, my new code uses

from gi.repository import Gtk
from gi.repository import AppIndicator3 as appindicator

as shown here https://wiki.ubuntu.com/DesktopExperienceTeam/ApplicationIndicators#Python_version

The full code I am trying to convert is here http://pastebin.com/raw.php?i=aUtASRpy

What is the equivalent of

gtk.timeout_add(PING_FREQUENCY * 1000, self.doWork)

in PYGI?

I am also assuming PYGI is the most modern way to make a unity indicator

I also assuming that it is best for the indicator to check the state of something rather than for something to tell the indicator the state (push vs pull). Is it okay to have a python program checking something every second?


2 Answers 2


Just to make sure that the Gtk way is also present in an answer:

The equivalent of

gtk.timeout_add(PING_FREQUENCY * 1000, self.doWork)

in gobject introspection (PyGI) is:

from gi.repository import GLib
GLib.timeout_add(PING_FREQUENCY * 1000, self.doWork)

However, when checking something regularly every x seconds, you should use

GLib.timeout_add_seconds(PING_FREQUENCY, self.doWork)

This allows the Glib to group timers and therefore save power, which is important for mobile devices. From the documentation:

The grouping of timers to fire at the same time results in a more power and CPU efficient behavior so if your timer is in multiples of seconds and you don't require the first timer exactly one second from now, the use of glib.timeout_add_seconds() is preferred over glib.timeout_add().

  • 4
    This seems way better than using threading. Especially since threading can cause weird bugs and lockups if you don't really know what you are doing.
    – Timo
    Dec 14, 2012 at 11:49

It is better to ask one question per... err... question.

Python's standard library has threading module which has a Timer class which does exactly what you need. Documentation.

Regarding push vs pull - it is definitely better when your application receives a notification when something happens (push) instead of checking if something happened every second (pull) - "are we there yet? are we there yet? are we there yet?.." - because it allows your app to just sleep and do nothing until it's notified, as opposed to doing the same repeating check every second. The thing is, however, that for some types of activities it may be tricky to get a notification, so it depends on the nature of your application. Doing checks too often is also bad for battery life and other stuff.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.