Objective
Create a Python program that shares time between several tasks in turns (round-robin), as a task scheduler does. Each task is a generator: it runs one step and hands over control with yield.
Instructions
- Write the generator
task(name, steps): for eachifrom 1 tostepsit showsname: step iand doesyield; when it ends it showsname: finished. - Read
nand thennlines withname steps. Create the tasks and store them in a queue (collections.deque). - While there are tasks: take the first one, advance it one step with
next()and, if it has not finished, put it back at the end of the queue. If it raisesStopIteration, it has finished and is not put back.
2
a 2
b 3
a: step 1
b: step 1
a: step 2
b: step 2
a: finished
b: step 3
b: finished
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise