Paul's Python CoursePPC

Project 10 of 15, for Neil

Task Scheduler

A pit-stop scheduler for Neil's kart on race day: the most urgent job always next, first come first served on a tie, on a heap built by hand from a list.

About 70 minutes, in 13 steps.

Sign up free to open itLog in

Act 4 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • NeilIt's race day at the kart track, and between sessions the jobs come in faster than I can do them.
  • NeilA loose steering bolt can't wait, but a cracked number plate can wait all day.
  • NeilIf two are just as urgent they get done in the order they came in, which is only fair.
  • PaulThen you want a scheduler that keeps the most urgent job on top, whatever comes in.

A task scheduler for Neil's pit crew. Jobs on the kart come in all race day with a priority: 1 is the most urgent. Neil always does the most urgent one waiting, and of two with the same priority, the one that came in first. In scheduler.py, by hand, with no heapq:

  1. push(heap, item) adds an item to a heap kept in a list, with the smallest item always at heap[0].
  2. pop(heap) removes the smallest item and hands it back. On an empty heap it raises IndexError.
  3. TaskScheduler, built on your push and pop: add(job, priority) schedules a job, take() hands back the one to do next (raising IndexError if there isn't one), and len(scheduler) says how many are waiting.

Both push and pop should take O(log n) steps, so a thousand jobs waiting cost about ten swaps a time. The main block replays Neil's race day from day.txt.

What it prints
Fixing the loose steering bolt
Fixing the brake pads
Fixing the tyre pressures
Fixing the fuel leak
Fixing the seat bolts
Fixing the chain tension
Still waiting: 1

What you learn

What's in it

  1. Smallest on top
  2. Smallest on top
  3. A tree kept in a list
  4. A 0 comes in
  5. Taking the top
  6. Which child?
  7. Most urgent, then first come
  8. Comparing tuples
  9. How Paul keeps his jobs board
  10. Fix Riya's version
  11. Make the task scheduler
  12. Ship it