Kamaelia docs : Axon.background

Running an Axon system in a separate thread

The background class makes it easy to run an Axon system in a separate thread (in effect: in the background).

This simplifies integration of Axon/Kamaelia code into other python code. See also Axon.Handle for a simple way to wrap a component in a thread safe way to access its inboxes and outboxes.

Example Usage

At its simplest, you could run a Kamaelia task independently in the background - such as a simple network connection, that dumps received data into a thread safe queue, after de-chunking it into lines of text.

NOTE: This example can be achieved more simply by using Axon.Handle. See the documentation of Axon.Handle to find out more.

  1. We implement a simple component to collect the data:

    from Axon.background import background
    from Axon.Component import component
    
    class Receiver(component):
        def __init__(self, queue):
            super(Bucket,self).__init__()
            self.q = queue
    
        def main(self):
            while 1:
                while self.dataReady("inbox"):
                    self.q.put(self.recv("inbox"))
                self.pause()
                yield 1
    
  2. Then we create a background object and call its start() method:

    from Axon.background import background
    
    background().start()
    
  3. Finally, we create and activate our Kamaelia pipeline of components, including the receiver component we've just written, passing it a thread-safe queue to put the data into:

    from Kamaelia.Chassis.Pipeline import Pipeline
    from Kamaelia.Internet.TCPClient import TCPClient
    from Kamaelia.Visualisation.PhysicsGraph import chunks_to_lines
    from Queue import Queue
    
    queue = Queue()
    
    Pipeline(
        TCPClient("my.server.com", 1234),
        chunks_to_lines(),
        Receiver(queue)
    ).activate()
    

We can now fetch items of data, from the queue when they arrive:

received_line = queue.get()

Behavour

Create one of these and start it running by calling its start() method.

After that, any components you activate will default to using this scheduler.

Only one instance can be used within a given python interpreter.

The background thread is set as a "daemon" thread. This means that if your program exits, this background thread will be killed too. If it were not a daemon, then it would prevent the python interpreter terminating until the components running in it had all terminated too.


Axon.background.background

class background(threading.Thread)

A python thread which runs the Axon Scheduler. Takes the same arguments at creation that Axon.Scheduler.scheduler.run.runThreads accepts.

Create one of these and start it running by calling its start() method.

After that, any components you activate will default to using this scheduler.

Only one instance can be used within a given python interpreter.

Feedback

Got a problem with the documentation? Something unclear that could be clearer? Want to help improve it? Constructive criticism is very welcome - especially if you can suggest a better rewording!

Please leave you feedback here in reply to the documentation thread in the Kamaelia blog.

-- Automatic documentation generator, 01 Feb 2010 at 04:00:28 UTC/GMT

 

Kamaelia is an open source project originated from and guided by BBC Research. For more information browse the site or get in contact.

This is an ongoing community based development site. As a result the contents of this page is the opinions of the contributors of the pages involved not the organisations involved. Specificially, this page may contain personal views which are not the views of the BBC. (the site is powered by a wiki engine)

(C) Copyright 2008 Kamaelia Contributors, including the British Broadcasting Corporation, All Rights Reserved

This web site is powered by the same code created for the bicker manor project. For more details, contact Michael Sparks at BBC Research directly (cf contact)