testCoder
testCoder

Reputation: 7385

What is the benefit of usage AsyncController?

I know that AsyncController created for multithreading goals. But i don't see any difference of behavior the Controller class and AsyncController class. For example HomeController:

public String First()
        {
            Thread.Sleep(5000);
            return "First";
        }

        public String Second()
        {
            return "Second";
        }

I try to execute /Home/First/ request in first tab of firefox, and after that i try to execute /Home/Second/ and i see that Second action executed immediately without any delay and without waiting for First action. It mean that requests executed in parallel threads and Controller class have multithreading support. And when i replace Controller with AsyncController i don't noticing the changes.

So my question is: what is the benefit of usage AsyncController, in which cases i should use that class?

Upvotes: 0

Views: 1216

Answers (1)

archil
archil

Reputation: 39491

There may be cases, when user request would be waiting for large IO operation, for example downloading content from another site. If controller serving this request is synchronous, then thread will be waiting for IO operation to complete, and server resources are wasted by that. Servers have limited number of threads to server, and if multiple users request same operation with large IO overhead, server may reach its thread limit and application will stop responding to further requests.

What asynchronous controller does, is that it does not make thread wait for IO operation to complete. It triggers IO operation and frees the thread, and pulls it back once operation is complete. During the operation progress, thread is free and may serve requests of other users, so it is not wasted any more, and there're less chances of application hang.

It should be noted that asynchronous controllers are not meant to be used to make code execution faster, as they do not do that.

Upvotes: 3

Related Questions