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:
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: class printer(microprocess): 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 : myscheduler.activateMicroprocess(X) 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(): 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.