ASP.NET MVC How to correctly use the asynchronous programming techniques

First, what is synchronous and asynchronous?

Synchronization (English: Synchronization), refers to events that occur in a system for coordination between the (event), appears consistency and unity of phenomena in time. It means to perform multiple tasks one by one, at the same time only one task execution.

Asynchronous (English: Asynchronization), refers to the CPU to respond to shelve the current request, process the next request, and when get a callback notification by polling or otherwise, began to run. Multi-threaded asynchronous operations will run into another thread, obtained by polling or callback method completion notification, but the port is completed, the scheduling of asynchronous operations taken over by the operating system, hardware interrupt is triggered upon completion callback method, which is not a You need to take additional threads.

Second, ASP.NET MVC project we should use asynchronous when the controller?

2.1, ASP.NET MVC why it requires the use of asynchronous?

IIS has a thread pool to handle the user's request, when a new request came, the thread scheduling pool to handle the request, however, but in the case of high concurrency, thread pool is not able to meet so many requests when each thread in the pool are busy status at the time of processing the request would process the request thread is blocked, and the thread can not provide services for another request, if the request queue is full, the Web server will reject HTTP 503 requests and in busy state. If it is to deal with some high latency, such as network operations, most of this thread is just waiting for the state most of the time do not do anything, so the thread can use asynchronous programming to better take advantage of.

Third, the use of synchronous and asynchronous scenarios

Describe a scene: If a request to generate a second or two to complete the required network calls, the request to perform either synchronous or asynchronous execution requires two seconds. However, during an asynchronous call, the server while waiting for the completion of the first request does not block the response to other requests. Therefore, when there is a request to call the operation a number of long-running, asynchronous requests can prevent the queued requests appear.

Scene Description Two: Suppose I have three operations are time-consuming 500, 600 and 700 msec. Synchronous call, then a total response time will be slightly more than 18 milliseconds. However, if the call is asynchronous (concurrent), total response time will be slightly more than 700 milliseconds, because that is the longest of the mission / operation. Therefore: When an action must be performed multiple independent operating long-running, asynchronous action method is very useful.

3.1 synchronous pipeline when the following conditions:

1), the operation is very simple or very short operating time.

2), simplicity is more important than efficiency.

3) This operation is mainly CPU operation instead of containing a large amount of disk or network operating overhead. For CPU-bound operations using asynchronous operation method does not provide any benefit and also results in more overhead.

 

3.2, using asynchronous pipeline when the following conditions:

1), or operation is bound to the network I / O bound instead of CPU-bound.

2), tests showed blocking operation for the site is a performance bottleneck, and by the use of these blocking calls an asynchronous method of operation, IIS can service more requests.

Parallelism is more important than simple code.

3) You want to provide a mechanism that allows the user to cancel the long-running request.

 

Four, Q & A link

4.1, since it can greatly offer asynchronous application responsiveness? So if all of the ASP.NET MVC controller using asynchronous (Async  the Controller ), what effect? It will become a high-throughput, high concurrency site it?

Just add the code async fact will not improve any performance, it is necessary (IO) asynchronous execution can really enhance the throughput required asynchronous place. Controller used for asynchronous I / O intensive operations, such as read and write data, and more independence between the operation; CPU-intensive and do not apply to asynchronous operation - whether you deal with asynchronous or synchronous processing, CPU eventually will be filled top . So asynchronous operation can indeed achieve the effect of increasing the number of concurrent, but the specific use will depend on where you put it. All use asynchronous Controller does not reach the absolute effect of improving site performance.

 

Guess you like

Origin www.cnblogs.com/wangyinlon/p/11961530.html