Priority Queues and Async Tasks

I started playing with Priority Queues last week, they are very simple to use. Create a queue, add an element, assign a priority to that element.

PriorityQueue<string, int> myQueue = new PriorityQueue<string, int>();

myQueue.Enqueue("middle", 2);
myQueue.Enqueue("first", 1);
myQueue.Enqueue("last", 3);

As you might guess, when dequeuing these they will come out in the order of “first”, “middle”, and “last”. Very nice, very easy.

Running async tasks based on their priority

In the past month, I’ve written a few blogs about using WhenAny and wanted to see if I could combine what I was doing there with priority queues. Here is the contrived example that resulted.

I don’t have a practical use for this, but I’m guessing that someone out there does.

I have three async methods MakeBigWidget, MakeMediumWidget, and MakeSmallWidget that all include a random delay, but usually, the the big one takes the longest, and the small one takes the shortest amount of time.

Into the priority queue I put the three Func<Task>, setting the big one as the highest priority, and the small one as the lowest priority (lines 5-9).

Then I loop over the priority queue, dequeuing the highest priority item, run task, and add to a List<Task> (lines 13-18).

Finally, I wait for each task to complete and remove the task from the List<Task> (lines 21-28).

There it is, hopefully this will solve a real problem someone has.

 1public class PriorityQueueForAsyncTasks
 2{
 3    public static async Task Main()
 4    {
 5        PriorityQueue<Func<Task>, int> widgetPriorityQueue = new PriorityQueue<Func<Task>, int>();
 6
 7        widgetPriorityQueue.Enqueue(MakeSmallWidget, 3);
 8        widgetPriorityQueue.Enqueue(MakeMediumWidget, 2);
 9        widgetPriorityQueue.Enqueue(MakeBigWidget, 1);
10
11        List<Task> widgetsBeingPrepared = new List<Task>();
12
13        while(widgetPriorityQueue.Count > 0)
14        {
15            var widgetToStartOn = widgetPriorityQueue.Dequeue();
16            Console.WriteLine($"Starting on {widgetToStartOn.Method.Name}");
17            
18            widgetsBeingPrepared.Add(Task.Run(() => widgetToStartOn()));
19        }
20
21        while(widgetsBeingPrepared.Any())
22        {
23            var widgetThatIsReady = await Task.WhenAny(widgetsBeingPrepared);
24
25            await widgetThatIsReady;
26            
27            widgetsBeingPrepared.Remove(widgetThatIsReady);
28        }
29    }
30
31    public static async Task MakeSmallWidget()
32    {
33        await Task.Delay(random.Next(100, 400));
34        Console.WriteLine("Small widget is ready");
35    }
36    
37    public static async Task MakeMediumWidget()
38    {
39        await Task.Delay(random.Next(200, 400));
40        Console.WriteLine("Medium widget is ready");
41    }
42
43    public static async Task MakeBigWidget()
44    {
45        await Task.Delay(random.Next(300, 600));
46        Console.WriteLine("Big widget is ready");
47    }
48    static Random random = new Random();
49}
comments powered by Disqus

Related