Home  >  Article  >  Backend Development  >  How to correctly use asynchronous programming technology in ASP.NET MVC

How to correctly use asynchronous programming technology in ASP.NET MVC

黄舟
黄舟Original
2017-09-07 13:40:422395browse

1. What are synchronization and asynchronousness?

Synchronization (English: Synchronization) refers to the coordination between events that occur in a system, and the phenomenon of consistency and unification in time. To put it bluntly, multiple tasks are executed one by one, and only one task is executed at the same time.

Asynchronous (English: Asynchronization) refers to letting the CPU temporarily put aside the response to the current request, process the next request, and start running after receiving a callback notification through polling or other methods. Multi-threads put asynchronous operations into another thread to run, and get completion notifications through polling or callback methods. However, on the completion port, the operating system takes over the scheduling of asynchronous operations, and triggers the callback method when it is completed through hardware interrupts. This method does not Additional threads are required.

2. When should we use asynchronous controllers in ASP.NET MVC projects?

2.1. Why do we need to use asynchronous in ASP.NET MVC?

IIS has a thread pool to handle user requests. When a new request comes, the threads in the pool will be scheduled to process the request. However, the amount of concurrency is very high. When the threads in the pool are no longer able to satisfy so many requests, and each thread in the pool is busy, the thread processing the request will be blocked when processing the request, and the thread cannot serve another request. , if the request queue is full, the web server rejects the request and is in HTTP 503 busy state. If you are dealing with some high delays, such as network operations, most of these threads just wait in the state and do nothing most of the time. Such threads can be better utilized using asynchronous programming.

3. Synchronous and asynchronous usage scenarios

Scenario description one: If a request generates a network call that takes two seconds to complete, whether the request is executed synchronously or Both asynchronous executions take two seconds. However, during an asynchronous call, the server does not block responding to other requests while waiting for the first request to complete. Therefore, asynchronous requests prevent request queuing when there are many requests calling long-running operations.

Scenario description two: Suppose I have three operations, which take 500, 600 and 700 milliseconds respectively. With synchronous calls, the total response time will be slightly more than 1800 milliseconds. However, if it is an asynchronous call (concurrent), the total response time will be slightly more than 700 milliseconds because that is the duration of the longest task/operation. Therefore: Asynchronous action methods are useful when an action must perform multiple independent long-running operations.

3.1. Use the synchronous pipeline when the following conditions are met:

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

2), simplicity is more important than efficiency.

3) This operation is mainly a CPU operation rather than an operation that involves a large amount of disk or network overhead. Using asynchronous operation methods for CPU-bound operations provides no benefit and also results in more overhead.



3.2. Use asynchronous pipelines when the following conditions are met:

1), Operations are network-bound or I/O-bound rather than CPU-bound.

2), testing shows that blocking operations are a bottleneck for website performance, and by using asynchronous operation methods for these blocking calls, IIS can serve more requests.

Parallelism is more important than code simplicity.

3), you want to provide a mechanism that allows users to cancel long-running requests.



4. Q&A session

4.1. Since asynchronous can greatly improve the responsiveness of the application? So what will be the effect if ASP.NET MVC all uses asynchronous controllers (Async Controller)? Will it become a high-throughput, high-concurrency website?

Just adding async to the code will not actually bring any performance improvement. It must be executed asynchronously where asynchronous is required (IO) Only in this way can the throughput be truly improved. Asynchronous Controllers are mostly used for I/O-intensive operations, such as reading and writing data, and the operations are relatively independent; CPU-intensive operations are not suitable for asynchronous operations - whether you are processing asynchronously or synchronously, the CPU will eventually be filled. . So asynchronous operations can indeed achieve the effect of increasing the number of concurrency, but it depends on where you use it. Using all asynchronous Controllers will not definitely improve site performance.

The above is the detailed content of How to correctly use asynchronous programming technology in ASP.NET MVC. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn