Table of Contents
1. What are synchronization and asynchronousness?
2. When should we use asynchronous controllers in ASP.NET MVC projects?
3. Synchronous and asynchronous usage scenarios
4. Q&A session
Home Backend Development C#.Net Tutorial How to correctly use asynchronous programming technology in ASP.NET MVC

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

Sep 07, 2017 pm 01:40 PM
asp.net asynchronous use

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Quick Application: Practical Development Case Analysis of PHP Asynchronous HTTP Download of Multiple Files Quick Application: Practical Development Case Analysis of PHP Asynchronous HTTP Download of Multiple Files Sep 12, 2023 pm 01:15 PM

Quick Application: Practical Development Case Analysis of PHP Asynchronous HTTP Download of Multiple Files With the development of the Internet, the file download function has become one of the basic needs of many websites and applications. For scenarios where multiple files need to be downloaded at the same time, the traditional synchronous download method is often inefficient and time-consuming. For this reason, using PHP to download multiple files asynchronously over HTTP has become an increasingly common solution. This article will analyze in detail how to use PHP asynchronous HTTP through an actual development case.

How Swoole supports asynchronous SMTP operations How Swoole supports asynchronous SMTP operations Jun 25, 2023 pm 12:24 PM

With the continuous development and popularization of the Internet, email has become an indispensable part of people's lives and work, and SMTP (Simple Mail Transfer Protocol) is one of the important protocols for email sending. As an asynchronous network communication framework for PHP, Swoole can well support asynchronous SMTP operations, making email sending more efficient and stable. This article will introduce how Swoole supports asynchronous SMTP operations, including using sync

How Swoole supports asynchronous AMQP operations How Swoole supports asynchronous AMQP operations Jun 25, 2023 am 08:22 AM

As the volume of Internet business continues to grow, the demand for high concurrency and high performance is getting higher and higher, and Swoole, as a network communication framework for PHP, is increasingly favored by developers. Among them, Swoole supports asynchronous AMQP, which is one of the more common application scenarios. So let's take a look at how Swoole supports asynchronous AMQP operations. First, we need to clarify what AMQP is. AMQP (AdvancedMessageQueuingProtocol) Advanced

Advanced Guide to Python asyncio: From Beginner to Expert Advanced Guide to Python asyncio: From Beginner to Expert Mar 04, 2024 am 09:43 AM

Concurrent and Asynchronous Programming Concurrent programming deals with multiple tasks executing simultaneously, asynchronous programming is a type of concurrent programming in which tasks do not block threads. asyncio is a library for asynchronous programming in python, which allows programs to perform I/O operations without blocking the main thread. Event loop The core of asyncio is the event loop, which monitors I/O events and schedules corresponding tasks. When a coroutine is ready, the event loop executes it until it waits for I/O operations. It then pauses the coroutine and continues executing other coroutines. Coroutines Coroutines are functions that can pause and resume execution. The asyncdef keyword is used to create coroutines. The coroutine uses the await keyword to wait for the I/O operation to complete. The following basics of asyncio

Asynchronous and non-blocking technology in Java exception handling Asynchronous and non-blocking technology in Java exception handling May 01, 2024 pm 05:42 PM

Asynchronous and non-blocking techniques can be used to complement traditional exception handling, allowing the creation of more responsive and efficient Java applications: Asynchronous exception handling: Handling exceptions in another thread or process, allowing the main thread to continue executing, avoiding blocking. Non-blocking exception handling: involves event-driven exception handling when an I/O operation goes wrong, avoiding blocking threads and allowing the event loop to handle exceptions.

How to use ThinkPHP6 for asynchronous logging operations? How to use ThinkPHP6 for asynchronous logging operations? Jun 12, 2023 am 09:57 AM

With the rapid development of the Internet, logging services have become an essential module for every large-scale web application. In order to facilitate various needs such as error troubleshooting and performance monitoring, this article will introduce how to use the ThinkPHP6 framework to perform asynchronous logging operations. 1. What is logging? In the field of computer science, logging refers to recording events and information that occur in a computer system. Typically, these records are stored in files or databases. Logging helps to understand the system operating status, detect and solve problems in a timely manner

How Swoole supports asynchronous SSH operations How Swoole supports asynchronous SSH operations Jun 25, 2023 am 11:10 AM

Swoole is a PHP extension designed for high concurrency, which can greatly improve the performance of PHP. It supports asynchronous IO, coroutine, multi-process and other features, and performs well in network programming and high-load scenarios. This article will introduce how Swoole supports asynchronous SSH operations. 1. Introduction to SSH SSH (SecureShell) is an encrypted network protocol used to securely transmit information on the network. The SSH protocol is safe, reliable, and cross-platform, and is widely used in remote login, file transfer,

Asynchronous data exchange using Ajax functions Asynchronous data exchange using Ajax functions Jan 26, 2024 am 09:41 AM

How to use Ajax functions to achieve asynchronous data interaction With the development of the Internet and Web technology, data interaction between the front end and the back end has become very important. Traditional data interaction methods, such as page refresh and form submission, can no longer meet user needs. Ajax (Asynchronous JavaScript and XML) has become an important tool for asynchronous data interaction. Ajax enables the web to use JavaScript and the XMLHttpRequest object

See all articles