Ubuntu – PYGTK to PYGI: gtk.timeout_add equivalent

application-developmentindicatorprogrammingpygipython

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?

Thanks!

Best Answer

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.