Asp. Net asynchronous programming-is performance improved when Asynchronization is used?

Source: Internet
Author: User

Asp. Net asynchronous programming

I haven't written a Blog for a long time. No, actually, I haven't written a Blog for the same purpose. Today I am free to write a Blog.

With. the launch of Net4.5, a new programming method that simplifies asynchronous programming, from time to time on the Internet also see a variety of playing Asp. net asynchronous programming slogan, how to improve performance, how to improve throughput!

Many articles are unclear or even incorrect. I only see some performance and obfuscation concepts. I hope this article can be used to understand the Asp.net asynchronous programming model for some people.

1. Basic Knowledge: it is not very basic for beginners to understand basic knowledge.

First look at this code

ThreadPool. GetMaxThreads (out workerThreads, out completionPortThreads );
ThreadPool. GetAvailableThreads (out workerThreads, out completionPortThreads );

Asp.net has two types of threads, one is the working thread, and the other is the IO thread, also known as the completion port thread. to put it simply, the worker thread is the thread that processes common requests, and the thread that is most used in common code.

This thread is limited, and is related to the number of root CPUs. IO threads, such as file read/write and network operations, can achieve real performance enhancement asynchronously [asynchronous].

If this IO thread is not specifically processed, it is usually not processed. This IO thread is basically idle.

I/O threads can be used to replace working threads, because they are limited and precious to process user requests.

2 ThreadPool and Task are actually all threads. For Asp.net, the Code does not do any special processing, usually working threads, and the threads in the thread pool

Thread is the underlying Thread and is used directly without any encapsulation. It is time-consuming to create this Thread and is not easy to reuse.

3 async/await a new syntactic sugar, a simplified asynchronous programming model, is recommended. with this, our asynchronous programming model becomes simple and elegant-this is closely related to tasks, how can we... practice by yourself

After learning about the above concepts, we use best practices to improve performance and throughput.

The following is an example of a WebApi.

 

        public async Task<string> Get()        {            return await GetArticleContentAsync();        }        private async Task<string> GetArticleContentAsync()        {            using (var httpClient = new HttpClient())            {                var response = await httpClient.GetAsync("http://www.asp.net");                var buffer = await response.Content.ReadAsByteArrayAsync();                return Encoding.UTF8.GetString(buffer);            }        }

This code looks similar to other blogs on the Internet, but this method is best for asp.net Asynchronization, improving throughput by 1st using the I/O port, [retrieving data from a http://www.asp.net] when processing network requests

The processing thread is returned to the thread pool so that it can process requests from other users. When retrieving data from www.asp.net, only one IO thread is occupied.

 

List the items related to other blogs on the Internet.

 public async Task<string> GetArticleContentByNoRigntWayAsync()        {            return await Task.Run(() =>                {                    using (var client = new WebClient())                    {                        return client.DownloadString("http://www.asp.net");                    }                });        }

This Code seems to be no different from the above Code, but this code is essentially different from the first method above. Is the performance actually improved? Can it actually increase the throughput? This is also true for many developers.

I will give the answer here first. This method [using GetArticleContentByNoRigntWayAsync] is unlikely to improve performance, especially in the Asp.net environment.

This is indeed used for Asynchronization, as well as for tasks and thread pools. It is only used.

I want to know why I didn't improve performance or throughput. I need your objective support.

Then I did not finish it yesterday. Continue!

When the synchronous method is closed to asynchronous mode, Asp.net only occupies the thread pool of the thread pool, and may also cause inter-thread switching. As for the time consumed for switching the thread pool, I don't know, but we are already focusing on performance issues.

Thread switching should be avoided. The total switching time is longer than the No switching time, right?

We need to use parallel computing. We can directly use synchronization, and add a few tasks. If there is only one Task, synchronization is unnecessary.

So let me summarize our usage.

To use Asynchronization, I/O threads must be used to complete the operation.

If I/O threads are not used, it would be better to directly use synchronization.

 

1 asynchronous + IO thread

 

2. Direct Synchronization

 

3 parallel computing [taking full advantage of CPU] synchronization + more than two tasks

The throughput of a in parallel will decrease. If the CPU is idle,You can consider implementing a Thread pool by yourself [using Thread], which is usually not easy to write stably.

B [recommended] Increase the default ThreadPool quantity, provided that the CPU is idle.

 

At the end of the article, eight people recommended this Blog and five people opposed it-> the article is not well written. Sorry, everyone.

 

 

 

If you think there is more value, click the lower right cornerRecommendation

 

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.