I have a 1 core machine and I would like to improve my code performance using async and await. The code has two major parts. the first an IO operation. (reading from azure service bus queue for multiple sessionId), the second, processing the data - CPU heavy. I have wrapped the DequeueAsync method in an async method which returns a Task:
private async Task<SomeReturnValue> AcceptFromQueueAsync(){
try{
SomeReturnValue result = await DequeueAsync.configureAwait(false);
return SomeReturnValue;
}
catch{
//logging and stuff
}
The CPU heavy method is sync: DoSyncWork() Since the second part is CPU heavy I don't want to use parallelism (actually can't... ) The CPU part can only start when the IO part finishes. Given the above is the following implementation the way to go with 1 cpu machine?
private void AcceptAndProcessWrapper(){
//Count is some cosnt defined outside the method
_acceptTasks = new List<Task<SomeReturnValue>>();
for (var i = 0; i < Count; i++)
{
_acceptTasks.Add(AcceptFromQueueAsync());
}
//The use of list is for convince in processing
Task.WaitAll(_acceptTasks.ToArray());
//The CPU part
Task.Run(DoSyncWork).Wait();
}
I know that I could have not use the Task.Run() for the sync part but I want to allow feature implementation on multiple cores (By starting several Tasks using Task.Run() and holding them in an array) Will the above implementation. (The multiple calls to async method that returns a task) improve the performance? Or should I start a new Task for each async call e.g Task.Run(AcceptFromQueueAsync)?