Concurrency · 12 / 15 Exercise Hard

Worker pool

Objective

A worker pool (like ThreadPoolExecutor) shares a list of jobs among a fixed number of workers: each job is taken by the first one that becomes free. Compute when each one starts and ends with a virtual clock. Real threads cannot be created in the browser, so the program simulates the situation in a deterministic way.

Instructions

  • Read the number of workers k, the number of jobs n and the duration of each job (one per line).
  • All the workers are free at time 0. Jobs are assigned in order to the worker that becomes free first (in case of a tie, the one with the lowest number). Use a heap with heapq.
  • For each job show Job j: worker w, from t to f (times in the given unit). At the end show Total time: T: the moment the last one finishes.
Input
2
5
4
3
5
2
6
Output
Job 1: worker 1, from 0 to 4
Job 2: worker 2, from 0 to 3
Job 3: worker 2, from 3 to 8
Job 4: worker 1, from 4 to 6
Job 5: worker 1, from 6 to 12
Total time: 12

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise