The kaa main loop is defined in the kaa-base in a submodule kaa.notifier. It is based on the generic notifier of pyNotifier. If pyNotifier is installed, the kaa main loop will dock itself into the pyNotifier main loop if possible.

Depending on what you currently use, some small steps need to be made to make the notifier loop running. If you aren't using a main loop right now, you should use the kaa main loop. You can find more details at SourceDoc/KaaNotifier about how to use the module.

GTK

The generic notifier is compatible with the GTK/Glib mainloop and kaa notifier has a special handler to hook itself into the GTK/Glib mainloop. Kaa notifier will use the GTK/Glib mainloop when gtk or gobject is imported once the mainloop is active. But it is possible to force the notifier loop to use GTK/Glib by calling init.

import kaa.notifier
kaa.notifier.init('gtk')

This will the the GTK mainloop (the GTK mainloop is based on the glib mainloop but is a bit different). If you want the glib and not the GTK based mainloop add x11 = False to init.

If pyNotifier is installed it will be used to run the mainloop; usage of packages requiring pyNotifier and not kaa.notifier is possible.

Twisted

Since 1.4 kaa notifier defines a Twisted reactor to integrate the Twisted mainloop into the kaa mainloop. After installing the reactor you can either run kaa.main() or reactor.run() to start the mainloop. Due to the internal design of Twisted you can not stop the mainloop from Twisted callbacks by calling sys.exit() or kaa.notifier.shutdown(), you need to call reactor.stop(). From kaa callbacks sys.exit() and kaa.notifier.stop() is supported.

# install special kaa reactor
import kaa.notifier.reactor
kaa.notifier.reactor.install()

# get reactor
from twisted.internet import reactor

# add callbacks to Twisted or kaa.notifier
# see test/twisted_in_kaa.py in the kaa.base package

# you can either call kaa.main() or reactor.run()
kaa.main()

The Twisted reactor will work with any kaa.notifier backend (generic and gtk).

There is also the reverse option putting the kaa mainloop into Twisted and let the Twisted reactor run. This is based on the thread notifier described below and will not use an external pyNotifier installation.

# get reactor
from twisted.internet import reactor

import kaa.notifier
kaa.notifier.init('twisted')

# add callbacks to Twisted or kaa.notifier
# see test/kaa_in_twisted.py in the kaa.base package

# run Twisted mainloop
reactor.run()

Other mainloops

PyNotifier has wrappers for qt and wxwindows but they may not work as expected with other kaa modules. For that reasons they can not be selected. It is always possible to run the kaa.notifier mainloop in a thread but that also means that kaa modules and other parts of the code have a different idea what the mainloop is.

A different solution is the thread based notifier in kaa.notifier. In this mode the kaa mainloop will run in an extra thread and will call a callback to the real mainloop that should be called from the real main thead. The other mainloop only needs to support a callback function that will be called from a thread and will execute the argument (a function without parameter) from the mainloop. An extra argument can be provided for a clean shutdown if the kaa mainloop whats to shut down the system. If not callback is provided, kaa.notifier.shutdown will be called.

The following example will integrate the kaa mainloop in the normal Twisted reactor. In this case the Twisted mainloop is running, kaa.main() should not be called.

# get reactor
from twisted.internet import reactor

# start thread based mainloop and add Twisted callback
import kaa.notifier
kaa.notifier.init('thread', handler = reactor.callFromThread, 
                  shutdown = reactor.stop)

# add callbacks to Twisted or kaa.notifier
# see test/kaa_in_twisted.py in the kaa.base package

# run Twisted mainloop
reactor.run()

Note: the notifier step signal will only be called every step the kaa mainloop does and does not affect steps the real mainloop does. Future version of kaa.notifier may fix that problem.

If you create a wrapper to use kaa.notifier with a different notifier using this solution please send us an example so we can include support for that mainloop in the kaa distribution.

SourceDoc/MainLoop (last edited 2008-03-21 15:22:40 by localhost)