Capstone C: the workers

The queue of the previous lesson had one reader. A pool has several, and the number of readers is the number of jobs that can be in flight. The program of this lesson starts two workers over the jobs of lesson 127, one of which is broken on purpose, and prints every result with its id, so the order of the output does not depend on which worker finished first.

The pool

Two channels and three tasks: the job channel is bounded at two, the result channel is unbounded, the producer writes the jobs and completes, the workers read until the job channel ends, and a third task completes the result channel once every worker is done. The program then reads every result and prints the sorted list:

using System.Threading.Channels; Console.WriteLine("workers 1.0"); var jobs = new[] { new Job(1, 1000, 20), new Job(2, -5, 10), new Job(3, 2500, 30), new Job(4, 500, 10), new Job(5, 1500, 20), new Job(6, 300, 10), }; var queue = Channel.CreateBounded<Job>(2); var results = Channel.CreateUnbounded<JobResult>(); var producer = Task.Run(async () => { foreach (var job in jobs) { await queue.Writer.WriteAsync(job); } queue.Writer.Complete(); }); var workers = Enumerable.Range(1, 2).Select(id => Task.Run(async () => { await foreach (var job in queue.Reader.ReadAllAsync()) { await results.Writer.WriteAsync(await Work(job)); } })).ToArray(); var finish = Task.Run(async () => { await Task.WhenAll(workers); results.Writer.Complete(); }); await producer; var collected = new List<JobResult>(); await foreach (var result in results.Reader.ReadAllAsync()) { collected.Add(result); } await finish; foreach (var result in collected.OrderBy(result => result.Id)) { var detail = result.Ok ? $"{result.Sum}" : result.Error; Console.WriteLine($"job {result.Id} ok {result.Ok} {detail}"); } Console.WriteLine($"results {collected.Count} failed {collected.Count(r => !r.Ok)} total {collected.Where(r => r.Ok).Sum(r => r.Sum)}"); static async Task<JobResult> Work(Job job) { await Task.Delay(job.DelayMs); try { if (job.Units < 0) { throw new ArgumentOutOfRangeException(nameof(job), "units must not be negative"); } var sum = 0L; for (var i = 1; i <= job.Units; i++) { sum += i; } return new JobResult(job.Id, true, sum, ""); } catch (ArgumentOutOfRangeException ex) { return new JobResult(job.Id, false, 0, ex.Message); } } record Job(int Id, int Units, int DelayMs); record JobResult(int Id, bool Ok, long Sum, string Error);

dotnet run
echo "status: $?"

workers 1.0
job 1 ok True 500500
job 2 ok False units must not be negative (Parameter 'job')
job 3 ok True 3126250
job 4 ok True 125250
job 5 ok True 1125750
job 6 ok True 45150
results 6 failed 1 total 4922900
status: 0

Six results, one failure and the same total as the sequential run of lesson 127, which is the proof that the pool lost nothing. The failed job is the second one, with a negative number of units.

Work that can fail

Work is async and awaits Task.Delay, which gives the thread back to the pool while the job waits. That is the difference from the Thread.Sleep of lesson 127: with two workers and a blocking sleep, one worker would hold its thread for the whole delay, and a pool of two would be a pool of two threads and nothing more.

A worker that throws is a design decision, and the program makes it explicit: Work catches its own exception and returns a failed result. The alternative is to let the exception escape the worker task, where it stays inside the task until somebody awaits it, and lesson 080 showed what that looks like. A job that fails must not take the pool with it, so the failure becomes a value here.

Collecting the results

The reading loop ends when the result channel is completed, and the completion is the last act of the task that awaited the workers. This is the same shape as the queue lesson, one channel further along: a channel is a hand-off between stages, and a pipeline of two channels has three stages.

The sort by id is what makes the output stable. Without it the lines arrive in the order the workers finish, which is the subject of the next lesson; with it, a test can compare the whole output of the program.

The next lesson keeps the pool, gives every result the worker that produced it, and reports the totals per worker next to the totals of the run.

Exercises

  1. Run the pool with four workers and check that the total does not change.
  2. Make Work throw for zero units as well and add a second failing job to the table.
  3. Remove the sort and run the program three times, then describe what moves.
  4. Add the worker number to JobResult and print it next to the failed job.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why does a worker use Task.Delay and not Thread.Sleep?

    Answer

    Task.Delay gives the thread back to the pool while the job waits

    A blocking sleep would hold a thread for every waiting job.

  2. What ends the loop of a worker?

    Answer

    The producer completes the job channel and the last job is read

    ReadAllAsync ends at the completion of its own channel.

  3. What does this program print? using System.Threading.Channels; var channel = Channel.CreateBounded<int>(3); await channel.Writer.WriteAsync(1); await channel.Writer.WriteAsync(2); Console.WriteLine(await channel.Reader.ReadAsync());

    Answer

    1

    A channel is a queue, so the first item written is the first read.

  4. Why does the failed job not stop the pool?

    Answer

    Work catches its exception and returns a failed result

    The failure is a value in the result, not an escaping exception.

  5. What does this program print? try { throw new ArgumentOutOfRangeException("job", "units must not be negative"); } catch (ArgumentOutOfRangeException ex) { Console.WriteLine(ex.Message); }

    Answer

    units must not be negative (Parameter 'job')

    The runtime appends the parameter name to the message.

  6. What completes the result channel?

    Answer

    The task that awaits every worker, after the last one ends

    Complete must come after the last write, and the workers are the writers.

  7. What does this program print? var total = 0L; foreach (var n in new[] { 1000, 2500, 500, 1500, 300 }) { total += n * (n + 1L) / 2; } Console.WriteLine(total);

    Answer

    4922900

    That is the total of the five jobs that succeed in the measured run.

  8. Why is the collection sorted before it is printed?

    Answer

    The arrival order of results is not promised, and the output must be stable

    The next lesson shows the arrival order, which moves between runs.

  9. What does this program print? var results = new[] { (1, 500L), (2, 0L) }; Console.WriteLine(results.Count(r => r.Item2 == 0));

    Answer

    1

    One result of the measured run has a sum of zero, the failed job.

  10. What is the difference between a failed job and a failed worker?

    Answer

    The job failure is data the pool carries; a worker failure is a defect of the pool

    The catch inside Work turns the first into a result and leaves the second to the task.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev