The Kaa Application Framework

What are Kaa and kaa.base?

Kaa is an umbrella project comprising a set of Python modules, mostly inclined toward solving problems in the domain of multimedia.

kaa.base is an LGPL-licensed generic Python application framework, providing the foundation for other modules within Kaa, and can be used in any type of project, from small event-driven tools, to larger, complex applications.

The Kaa Application Framework includes a main loop facility with an API for signals and callables, timers, thread and subprocess management, local IO and sockets (with TLS support), inter-process communication, and much more.

Kaa also – and perhaps most importantly – provides a rich, practically magical API for asynchronous programming. Threads and coroutines in combination with InProgress objects, which are used extensively throughout Kaa, allow you to implement complex state machines responding to asynchronous events in very compact, readable code.

Here is a small taste of what an application written using Kaa looks like:

import time, socket, errno
import kaa

def timer():
    "This function is invoked every second from the main loop."
    print 'timer fired at', time.time()

def thread(count):
    "This function runs in a thread.  Notice it blocks."
    for i in range(count):
        print 'thread woke up at', time.time()

def coroutine():
    "By yielding, this function can have multiple reentry points."
    # We can spawn a new instance of thread().  The coroutine will reenter
    # after thread() finishes, but the main loop is not blocked; the timer
    # we started keeps firing all the while.
    print 'coroutine starting'
    yield thread(3)

    # Sub-process IO doesn't block.
    stdout, stderr = yield kaa.Process('lsusb').communicate()

    # And of course sockets don't block. Notice that asynchronously
    # generated exceptions can be handled as if you were writing typical
    # blocking code.
    sock = kaa.Socket()
        yield sock.connect('')
    except socket.error as e:
        print 'Connection failed:', e.strerror
        sock.write('GET / HTTP/1.0\n\n')
        webpage = yield
        print webpage

    # We can yield back to the main loop at any time.
    yield kaa.NotFinished

    # Or we can be reentered after some (non-blocking) period of time.
    yield kaa.delay(2)

    # Ok, let's shut everything down.  Main loop stops and coroutine exits.
    print 'coroutine done'

# Start a new thread that runs thread() inside it.
# Start the timed function
# Invoke the coroutine.  It will immediately execute everything before the
# first yield and then return, having scheduled itself for reentry.
# Start the main loop.  This blocks until explicitly stopped, or if
# KeyboardInterrupt or SystemExit is raised, or if there is an uncaught
# exception.

Where do I get kaa.base?

The easiest and recommended way to install kaa.base is using pip (available as the python-pip package in Ubuntu and Fedora):

sudo pip install --upgrade kaa-base

Or, if you prefer to install kaa.base as an egg using setuptools (package python-setuptools on Ubuntu and Fedora):

sudo easy_install -U kaa-base

The most up-to-date tree can be cloned with git:

git clone git://
cd kaa-base
sudo python install

The project is hosted at GitHub, so if you’d like to contribute, you can can fork it and send pull requests.

Your distribution might already have kaa.base included in its standard repositories, but be aware that these are almost certainly very out of date:

# For Ubuntu and Debian
sudo apt-get install python-kaa-base

# For Fedora
yum install python-kaa-base

Finally, source packages are available on GitHub.