April 2024 - This site, and Kamaelia are being updated. There is significant work needed, and PRs are welcome.

2. Scheduler - A means of running lots of microprocesses

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

Objects created shold have the following attributes:

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.

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)

Answer Hidden

Show Answer

Answer:

Discussion:

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:

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

Next we can create a 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:

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

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