0

I understand that async calls can be a useful tool under some circumstances. If you are able to do something else while an asynchronous call is processed, it can speed up your application.

However, in an MVC application running in IIS I don't see any benefit of code such as: var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe)

My understanding is that IIS is already allocating requests to the threads it has available and that spawning off a new thread won't buy anything if you always await. In this example, all we're doing is adding some overhead for context switching. The thread IIS allocated to handle the request is not freed up at any time because of the "await".

Am I missing something? Is the thread made available to IIS while we are waiting? If we always wait for a response from a method, is there any reaon to make it asynchronously?

  • 1
    In the case you mention, indeed there are benefits, since while you are waiting on IO operations the worker thread is actually returned to the pool, and IIS can handle more requests without starving the threadpool. Take a look at this for more info: http://stackoverflow.com/a/19087592/1373170 – Pablo Romeo Apr 20 '15 at 22:17
  • 2
    Yes, there is a small penalty you pay for switching context, but thread does return to the pool while I/O Operations, so it allows the application to be Scalable – pjobs Apr 20 '15 at 23:50

1 Answers1

0

spawning off a new thread won't buy anything if you always await

Most of the async methods that you encounter do not spawn a thread. Instead, they start some external action and then complete the Task when the action is complete.

The thread IIS allocated to handle the request is not freed up at any time because of the "await".

Except it is. Request handling is not tied to a thread. When you await, your current thread is released, free to handle other requests. When the await is done, handling of the request will continue on some thread from the pool (could be the same one as before, or it could be a different thread).

svick
  • 236,525
  • 50
  • 385
  • 514