The Blog of Ian Mercer.

Constrained parallelism for the Task Parallel Library

When developing .NET applications there is often the need to execute multiple background processes, for example, fetching and rendering different size thumbnails for images. Typically you queue actions like these onto the thread pool. But in the case of thumbnail generation you typically want to fetch a base image first and then perform the resize operations on it. If five web pages each request a different thumbnail size simultaneously you may end up fetching the same image five times before processing it. Of course, you can add file based locking around this to ensure that only the first once gets to fetch the data but it would be much better if you could instead instruct the Task Parallel Library to execute co-dependent tasks sequentially.

The new Task parallel library has continuations that allow one task to chain onto the end of a previous task but you still a way to track all the tasks currently active so you can find the other task to chain onto it. In a multi-threaded asp.net environment that's not so easy.

Below is a TaskFactory that gives you constrained parallelism allowing you to queue up tasks in such a way that no two tasks with the same key will execute in parallel. To use it you simply create a new TaskFactorySequentiallyByKey and then call StartNewChainByKey() with a suitable key, e.g. “RENDERimage12345.jpg”. This method returns a normal Task object that you can Wait on or add more continuations. All the usual TaskFactory constructor options are provided so you can have a different TaskScheduler, common cancellation token, and other options.

Note also that it expects an Action<CancellationToken> not just a plain Action. This is so your Action can be polite and monitor the cancellation token to know when to stop early. If you don’t need that you can always pass in a closure that tosses the CancellationToken, i.e. (token) => MyAction().

[csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Diagnostics;

namespace Utility { /// <summary> /// The TaskFactorySequentiallyByKey factory limits concurrency when actions are passed with the same key. Those actions are executed sequentially /// and never in parallel. /// </summary> /// <remarks> /// For example, you have an action to fetch an image from the web to a local hard drive and then render a specific size of thumbnail for it. /// The action includes code to check if the original image is already on disk, if not it fetches it. /// It then checks if the correct size thumbnail has been rendered, if not it renders it. /// You want to be able to fire off requests for thumbnails from multiple different asp.net web pages and ensure that any two requests for the /// same original image are executed sequentially so that the image is only fetched once from the web before both thumbnail renders run. /// </remarks> public class TaskFactorySequentiallyByKey : TaskFactory { /// <summary> /// Tasks currently queued based on key /// </summary> Dictionary<string, Task> inUse = new Dictionary<string, Task>();

public TaskFactorySequentiallyByKey() : base() { }

public TaskFactorySequentiallyByKey(CancellationToken cancellationToken) : base(cancellationToken) { }

public TaskFactorySequentiallyByKey(TaskScheduler scheduler) : base(scheduler) { }

public TaskFactorySequentiallyByKey(TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions) : base(creationOptions, continuationOptions) { }

public TaskFactorySequentiallyByKey(CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) : base(cancellationToken, creationOptions, continuationOptions, scheduler) { }

protected virtual void FinishedUsing(string key, Task taskThatJustCompleted) { lock (this.inUse) { // If the key is present AND it point to the task that just finished THEN we are done // and can clear the key for the next task that comes in ... if (this.inUse.ContainsKey(key)) if (this.inUse[key] == taskThatJustCompleted) { this.inUse.Remove(key); Debug.WriteLine("Finished using " + key + " completely"); } else { Debug.WriteLine("Finished an item for " + key); }

} }

/// <summary> /// Queue an action but prevent parallel execution of items having the same key. Instead, run them sequentially. /// </summary> /// <remarks> /// This allows you to, for example, queue up tasks to fetch an image from the web to a cache and render a thumbnail for it at different sizes /// while ensuring that the image is only fetched to the cache once before each different size thumbnail is generated /// </remarks> public Task StartNewChainByKey(string key, Action<CancellationToken> action) { return StartNewChainByKey(key, action, base.CancellationToken); }

/// <summary> /// Queue an action but prevent parallel execution of items having the same key. Instead, run them sequentially. /// </summary> /// <remarks> /// This allows you to, for example, queue up tasks to fetch an image from the web to a cache and render a thumbnail for it at different sizes /// while ensuring that the image is only fetched to the cache once before each different size thumbnail is generated /// </remarks> public Task StartNewChainByKey(string key, Action<CancellationToken> action, CancellationToken cancellationToken) { CancellationToken combined = cancellationToken == base.CancellationToken ? base.CancellationToken : CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, base.CancellationToken).Token;

lock (inUse) { Task result; if (inUse.TryGetValue(key, out result)) { // chain the supplied action after it ... result = result.ContinueWith((task) => action(combined), combined);

// And then schedule a completion check after that result.ContinueWith((task) => FinishedUsing(key, task));

// Update the dictionary so that it tracks the new LAST task in line, not any of the earlier ones inUse[key] = result;

Debug.WriteLine("Chained onto " + key);

return result; }

// otherwise simply create it and start it after remembering that the key is in use result = new Task(() => action(combined), combined);

inUse.Add(key, result);

// queue up the check after it result.ContinueWith((task) => FinishedUsing(key, task));

Debug.WriteLine("Starting a new action for " + key);

// And finally start it result.Start(this.Scheduler);

return result; } } } }

Related Stories

Cover Image for My love/hate relationship with Stackoverflow

My love/hate relationship with Stackoverflow

Stackoverflow is a terrific source of information but can also be infuriating.

Ian Mercer
Ian Mercer
Cover Image for Xamarin Forms Application For Home Automation

Xamarin Forms Application For Home Automation

Building a Xamarin Forms application to control my home automation system

Ian Mercer
Ian Mercer

JSON Patch - a C# implementation

Ian Mercer
Ian Mercer

Websites should stop using passwords for login!

A slightly radical idea to eliminate passwords from many of the websites you use just occasionally

Ian Mercer
Ian Mercer

Dynamically building 'Or' Expressions in LINQ

How to create a LINQ expression that logically ORs together a set of predicates

Ian Mercer
Ian Mercer

VariableWithHistory - making persistence invisible, making history visible

A novel approach to adding history to variables in a programming language

Ian Mercer
Ian Mercer

Neo4j Meetup in Seattle - some observations

Some observations from a meetup in Seattle on graph databases and Neo4j

Ian Mercer
Ian Mercer

Updated Release of the Abodit State Machine

A hierarchical state machine for .NET

Ian Mercer
Ian Mercer

My first programme [sic]

At the risk of looking seriously old, here's something found on a paper tape

Ian Mercer
Ian Mercer

Building a better .NET State Machine

A state machine for .NET that I've released on Nuget

Ian Mercer
Ian Mercer
Cover Image for The Internet of Dogs

The Internet of Dogs

Connecting our dog into the home automation

Ian Mercer
Ian Mercer

A simple state machine in C#

State machines are useful in many contexts but especially for home automation

Ian Mercer
Ian Mercer

Convert a property getter to a setter

Ian Mercer
Ian Mercer

MongoDB Map-Reduce - Hints and Tips

Ian Mercer
Ian Mercer
Cover Image for Weather Forecasting for Home Automation

Weather Forecasting for Home Automation

Ian Mercer
Ian Mercer

Lengthening short Urls in C#

Ian Mercer
Ian Mercer

Why don't you trust your build system?

Ian Mercer
Ian Mercer

ASP.NET MVC SEO - Solution Part 1

Ian Mercer
Ian Mercer

Elliott 803 - An Early Computer

Ian Mercer
Ian Mercer

Building sitemap.xml for SEO ASP.NET MVC

Ian Mercer
Ian Mercer

Continuous Integration -> Continuous Deployment

What is "quality" in terms of a released software product or website?

Ian Mercer
Ian Mercer

Making a bootable Windows 7 USB Memory Stick

Here's how I made a bootable USB memory stick for Windows 7

Ian Mercer
Ian Mercer

Tip: getting the index in a foreeach statement

A tip on using LINQ's Select expression with an index

Ian Mercer
Ian Mercer

SQL Server - error: 18456, severity: 14, state: 38 - Incorrect Login

A rant about developers using the same message for different errors

Ian Mercer
Ian Mercer

WCF and the SYSTEM account

Namespace reservations and http.sys, my, oh my!

Ian Mercer
Ian Mercer

404 errors on IIS6 with ASP.NET 4 Beta 2

Ian Mercer
Ian Mercer

Mixed mode assembly errors after upgrade to .NET 4 Beta 2

Fixing this error was fairly simple

Ian Mercer
Ian Mercer

The EntityContainer name could not be determined

How to fix the exception "the entitycontainer" name could not be determined

Ian Mercer
Ian Mercer

Shortened URLs should be treated like a Codec ...

Expanding URLs would help users decide whether or not to click a link

Ian Mercer
Ian Mercer

Tagging File Systems

Isn't it time we stopped knowing which drive our file is on?

Ian Mercer
Ian Mercer

A great site for developing and testing regular expressions

Just a link to a site I found useful

Ian Mercer
Ian Mercer

Introducing Jigsaw menus

A novel UI for menus that combines a breadcrumb and a menu in one visual metaphor

Ian Mercer
Ian Mercer

Entity Framework in .NET 4

Ian Mercer
Ian Mercer

Fix for IE's overflow:hidden problem

Ian Mercer
Ian Mercer

A better Tail program for Windows

A comparison of tail programs for Windows

Ian Mercer
Ian Mercer

Measuring website browser performance

Found this great resource on website performance

Ian Mercer
Ian Mercer

Amazon Instance vs Dedicated Server comparison

Some benchmark performance for Amazon vs a dedicated server

Ian Mercer
Ian Mercer

System.Data.EntitySqlException

Hints for dealing with this exception

Ian Mercer
Ian Mercer

Agile Software Development is Like Sailing

You cannot tack too often when sailing or you get nowhere. Agile is a bit like that.

Ian Mercer
Ian Mercer

Exception Handling using Exception.Data

My latest article on CodeProject covers the lesser known Exception.Data property

Ian Mercer
Ian Mercer

Javascript error reporting

Sending client-side errors back to a server for analysis

Ian Mercer
Ian Mercer

AntiVirus Software is the Worst Software!

When your anti-virus software starts stealing your personal data, it's time to remove it!

Ian Mercer
Ian Mercer

ASP.NET Custom Validation

How to solve a problem encountered with custom validation in ASP.NET

Ian Mercer
Ian Mercer

Optimization Advice

Some advice on software optimization

Ian Mercer
Ian Mercer

Linq's missing link

LinqKit came in handy back in 2009

Ian Mercer
Ian Mercer

Google Chart API

Ian Mercer
Ian Mercer

Cache optimized scanning of pairwise combinations of values

Using space-filling curves to optimize caching

Ian Mercer
Ian Mercer

Threading and User Interfaces

A rant about how few software programs get threading right

Ian Mercer
Ian Mercer

Take out the trash!

Why Windows shutdown takes so long

Ian Mercer
Ian Mercer

Dell upgrades - a pricey way to go

Ian Mercer
Ian Mercer

Programming mostly C#

Ian's advice on programming

Ian Mercer
Ian Mercer