Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async await calling long running sync AND async methods

I am using asp.net web api 2 and Entity Framework 6.

Original pseudo code

public IHttpActionResult GetProductLabel(int productId)
{
      var productDetails = repository.GetProductDetails(productId); 
      var label = labelCalculator.Render(productDetails);  
      return Ok(label);
}

Modified code

public async Task<IHttpActionResult> GetProductLabel(int productId)
{
      var productDetails = await repository.GetProductDetailsAsync(productId); // 1 long second as this call goes into sub services
      var label = labelCalculator.Render(productDetails); // 1.5 seconds synchrounous code
      return Ok(label);
}

Before my change everything ran synchronously.

After my change the call to a remote service which is calling again a database is done the async-await way.

Then I do a sync call to a rendering library which offers only sync methods. The calculation takes 1,5 seconds.

Is there still a benefit that I did the remote database_service call the async-await way but the 2nd call not? And is there anything I could still improve?

Note

The reason why I ask this is because:

"With async controllers when a process is waiting for I/O to complete, its thread is freed up for the server to use for processing other requests."

So when the first remote database_service call is processing and awaiting for that 1 second the thread is returned to IIS??!!

But what about the 2nd label calculation taking 1,5 seconds that will block the current thread again for 1,5 seconds ?

So I release and block the thread, that does not make sense or what do you think?

like image 833
Pascal Avatar asked Jul 24 '16 20:07

Pascal


1 Answers

The rendering library is not simply "blocking a thread", it is doing work performing your rendering. There is nothing better you can do.

like image 94
Scott Chamberlain Avatar answered Oct 10 '22 09:10

Scott Chamberlain