= A quick word about Axon and Kamaelia = == Introduction == [http://kamaelia.sourceforge.net/ Kamaelia] is a Python library by the [http://www.bbc.co.uk/opensource/projects/kamaelia/ BBC Research] for concurrent programming using a simple pattern of components that send and receive data from each other. Because an example is better than words, look at the following code snippet: {{{ #!python from Axon.Component import component from Axon.Ipc import shutdownMicroprocess, producerFinished class MyComponent(component): Inboxes = {"inbox" : "some data in", "control" : "stops the component"} Outboxes = {"outbox" : "some data out", "signal" : "Shutdown signal"} def __init__(self): super(MyComponent, self).__init__() def main(self): while 1: if self.dataReady("control"): mes = self.recv("control") if isinstance(mes, shutdownMicroprocess) or isinstance(mes, producerFinished): self.send(producerFinished(), "signal") break if self.dataReady("inbox"): data = self.recv("inbox") # let's echo what we received... self.send(data, 'outbox') if not self.anyReady(): self.pause() yield 1 }}} This is the simplest form a component can take. A component: * is a class that inherits from {{{Axon.Component.component}}} * has inboxes and outboxes By inheriting from {{{Axon.Component.component}}} you make your class usable by the [http://kamaelia.sourceforge.net/MiniAxon/ Axon] library which is at the core of the Kamaelia library. It allows for your class to be used with other components. Inboxes and outboxes allow your component to be linked to and from by other components. Then your class defines a main method that simple loop until a specific kind of message is put into the "control" inbox of the component. During the looping it checks for any inboxes and process data read from them. Eventually it yields to the Axon scheduler that goes to the next available component. By using a generator we allow the shceduler to come back to the component's loop eventually. Note that inboxes and outboxes are pure Python dictionary hence they allow for any Python objects and are not limited to strings. The component described above is simple, complex components have many inboxes and outboxes to link to and from. == Kamaelia == Kamaelia is a library of complex components for [http://kamaelia.sourceforge.net/Components all kind of tasks and topics] and is at the core of the [wiki:headstock headstock] product. For example taking the previous example we could write: {{{ #!python from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.Util.Console import ConsoleReader, ConsoleEchoer pipe = Pipeline(ConsoleReader(), MyComponent(), ConsoleEchoer()) pipe.run() }}} [http://kamaelia.sourceforge.net/Components/pydoc/Kamaelia.Experimental.Chassis.Pipeline.html Pipeline] is component that automatically links outboxes to inboxes of each provided component. The console components allow for reading and writing data from and to the command line. Because {{{Pipeline}}} is also a component itself it could in turns be used in another component. Note that calling the {{{run()}}} method on a component blocks the process until it is killed. You can also simply activate a component which will then be in an active state but will run only when eventually {{{run}}} is called on another component. In other words, the previous snippet could also be written like that: {{{ #!python from Kamaelia.Util.Console import ConsoleReader, ConsoleEchoer reader = ConsoleReader() reader.activate() mycomp = MyComponent() mycomp.link((reader, 'outbox'), (mycomp, 'inbox')) mycomp.activate() writer = ConsoleEchoer() mycomp.link((mycomp, 'outbox'), (writer, 'inbox')) writer.run() }}} Now that you have the basics of Kamaelia you should dive into its [http://kamaelia.sourceforge.net/Cookbook.html documentation] and have fun with its fantastic library.