is ASP. NET uses asynchronous performance to improve

Source: Internet
Author: User

ASP-Async Programming
With. The introduction of Net4.5, a new way of programming simplifies asynchronous programming, on the Internet, and occasionally see a variety of ASP. NET asynchronous programming, how to improve performance, how to improve throughput rate!
A lot of articles are not clear, even wrong. See only a few appearances, confusing concepts. It is hoped that this article will be able to understand the ASP. NET asynchronous Programming model.
The focus of this article is to understand how ASP. NET Async improves throughput and improves performance. Of course, the high performance is not only asynchronous, there are many ways, multithreading and so on.
1 basic knowledge, talk about a beginner is not easy to understand the basic knowledge, this basic knowledge, very non-basic OH
Look at this code first.
Threadpool.getmaxthreads (out workerthreads, out completionportthreads);
Threadpool.getavailablethreads (out workerthreads, out completionportthreads);
There are two types of threads in ASP. 1 classes are worker threads, the other is IO threads, and there is called completion port thread. To put it simply, the worker thread: the thread that handles the normal request, the thread most commonly used in the code.
This thread is limited and is related to the number of root CPUs. IO threads, such as read and write files, network operations, and so on can be asynchronous to achieve the real meaning of performance improvement [Async].
This IO thread, if not specifically handled, is usually not handled, and the IO thread is basically idle
It is possible to use the IO thread instead of the worker thread, because the processing user requests the worker thread, which is limited and relatively precious.
2threadpool,task These two are actually threads, for ASP. Code does not do special processing is usually a worker thread, thread constructor threads
Thread this is the underlying thread, without any encapsulation, to use directly, creating this thread is more time consuming and not easy to reuse.
3async/await a new syntactic sugar, a simplified mode of asynchronous programming model, is worth recommending. With this, our asynchronous programming model becomes simple, elegant--this is closely related to the task, how ... To practice on their own
After understanding the above concepts, we are using best practices to improve performance, throughput rates
An example of a webapi is given below
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 web, but this is the best way to increase throughput for ASP. 1th, using the IO port, when processing network requests [when retrieving data from http://www.asp.net]
The worker threads that were processed were returned to the thread pool so that they could handle requests from other users, and only one IO thread was consumed when fetching data from the network www.asp.net
Now list the other blogs on the internet about this piece of
Public async task<string> Getarticlecontentbynorigntwayasync ()
{
return await Task.run (() =
{
using (var client = new WebClient ())
{
Return client. downloadstring;
}
});
}
This code doesn't look any different from the code above, but there is an essential difference between the code and the first way above, does performance really improve? Is it really going to increase throughput rates? Many development is also used in this way
I'll give you an answer here, and this way [using Getarticlecontentbynorigntwayasync] is unlikely to improve performance, especially in an ASP.
This is true for async, and it also uses the task, thread pool. It just used to be.
Want to know why there is no performance improvement, no increase in throughput rate, you need the objective support
Then yesterday did not finish, continue to say!
The synchronization method is closed to asynchronous, in ASP. NET will only occupy the thread pool of thread pool, but also may cause the switch between threads, as for the thread pool switch time is not clear, I do not know, but already focus on performance issues, then we will
You should avoid thread switching, which is more time-consuming than switching, right?
We're going to use parallel computing, we use synchronization directly, plus a few tasks, and if there's only one task and it's synchronous, it's not necessary.
So for our use of words I summarize
Using async, use the IO thread and take advantage of this to complete the operation
If you are not using an IO thread, it is better to use synchronization directly
1 Asynchronous +io Threads
2 Direct Sync
3 Parallel computing [taking full advantage of CPU] Synchronization + two + tasks
A throughput rate decreases during parallel, and if the CPU is idle, consider implementing a thread pool yourself [using thread], which is often not easy to write stable
B[suggest] Change the default number of ThreadPool, if the CPU has idleTOEFL Answers

is ASP. NET uses asynchronous performance to improve

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.