We have a working implementation that is doing I/O operations, returning data from blobs in an async/await manner from Azure Blob Storage.
//Method1 is not async, this is what is called from the controller
public IEnumerable<Data> Method1()
{
//Running the async method and returning the result from task
return Task.Run(() => GetDataAsync()).Result;
}
private async Task<IEnumerable<Data>> GetDataAsync()
{
//There are multiple blob address where the data is held. The code creates, in parallel multiple tasks for each address.
//It returns tasks that will be run in Async pattern
var tasks = multipleBlobAddress.AsParallel.Select(blobAddress =>{
Task<IEnumerable<Data>> task = GetDataFromBlobsAsync(blobAddress);
return task;
});
//Awaits all tasks to complete
var completedTasks = await Task.WhenAll<IEnumerable<Data>>(tasks);
//Selects all the tasks and returns them. Each tasks has data.
return completedTasks.SelectMany(t => t);
}
private Task<IEnumerable<Data>> GetDataFromBlobsAsync(string address)
{
//Opens the blob and reads from it
using (var blobStream = blobService.OpenRead(address))
{
//Deserialises the data read from the blob
data = service.DeserialiseData(blobStream);
}
return Task.FromResult<IEnumerable<Data>>(data);
}
We have understood that the best way to read from blobs is to follow the async/await pattern and not using the AsParallel
method (other suggestions are most welcomed). I have the following questions:
- By creating tasks in parallel and then waiting for all of them to complete, do we lose any performance?
- What are the obvious areas where we got it wrong or we can improve and potentially increase the performance or make the code better to maintain/read?
- Are we correctly following the async / await pattern?
If you need any extra information about the code, I will happily provide it and hope to make it clearer.
await Task.WhenAll(IEnumerable<Data>(tasks));
appears to be invoking the typeIEnumerable<Data>
as a function. You should copy-paste the actual working code, not attempt to rewrite it from memory. \$\endgroup\$ – Peter Taylor Nov 2 '16 at 10:47