Take the 2-minute tour ×

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?


share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
Thank you, this lead me to hack together this pastebin.com/raw.php?i=0jLxZ994 with big help from stackoverflow.com/questions/9812344/… But, now time to learn how to make some sort of listener since with what I have in mind the majority of "polls" will discover that nothing has been changed, but, that is for another time. Thanks! –  onBootQuestioner Dec 14 '12 at 3:26

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().

share|improve this answer
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 '12 at 11:49

Your Answer


