2. Scheduler - A means of running lots of microprocesses

Exercise: Write a class called scheduler with the following characteristics.

  • It should subclass microprocess.

Objects created shold have the following attributes:

  • self.active - this is a list. (initially empty)
  • self.newqueue - this is also a list. (initially empty)
    Hint: Initialise these in the __init__ method!

Objects created should have the following methods:

  • __init__(self) - Perform any initialisation you need here (see above)
    Remember: Don't forget to called your super class's __init__ method!
  • main(self) - Takes no arguments
    This should be a generator with the following logic: (Looped 100 times)
    • Loop through all the objects in self.active using any mechanism you choose.
      • Suppose you call the current object (from self.active) current
      • Call current.next()
      • If a StopIteration exception is thrown, just catch and skip on to the next iteration. (eg continue)
      • If the result from current.next() was NOT -1, then append current onto self.newqueue
    • Having looped through all the objects, REPLACE self.active with self.newqueue, and replace the value of self.newqueue with a new empty list
  • activateMicroprocess(self, someprocess)
    • someprocess is a microprocess object (or anything that conforms to the same interface/behaviour seen by the scheduler).
    • This method should call the object's main method and append the result to self.newqueue

Answer HiddenShow Answer


  • Select from the above tabs to show the answer!


This class provides us with a rudimentary way of activating generators embedded inside a class, adding them to a runqueue and then letting something run them. So let's try it. The default microprocess is relatively boring, so let's create some microprocesses that are little more than an age old program that repeatedly displays a messae. To do that we declare a class subclassing microprocess and provide a generator called main. We'll also capture a provided argument:

    class printer(microprocess):
    def __init__(self, tag):
    super(printer, self).__init__()
    self.tag = tag
    def main(self):
    while 1:
    yield 1 # Must be a generator
    print self.tag

Note that this generator doesn't ever exit. We can then create a couple of these printers:

    X = printer("Hello World")
    Y = printer("Game Over") # Another well known 2 word phrase :-)

Next we can create a scheduler:

    myscheduler = scheduler()

We can then ask this scheduler to activate the two microprocesses - X & Y :


We can then run our scheduler by iterating through its main method:

    for _ in myscheduler.main():

If we run this we get the following output (middle of output snipped):

    >>> for _ in myscheduler.main():
    ... pass
    Hello World
    Game Over
    Hello World
    Game Over
    Hello World
    Game Over

As you can see, the scheduler hits the 100th iteration and then halts.

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)