Written in front of words, for a long time did not write a blog, no, in fact, there is no agreement on how to write. Today is free, I also write a blog
With. The introduction of Net4.5, a new programming way to simplify the asynchronous programming, on the Internet also see a variety of asp.net asynchronous programming slogan, how to improve performance, how to improve throughput!
Many articles are not clear, even wrong. See only a few manifestations, confusing concepts. Hopefully this article will be able to understand the ASP.net asynchronous programming model for some people.
1 basic knowledge, talk about a beginner is not easy to understand the basic knowledge, this basic knowledge, very basic OH
Look at this code first.
Threadpool.getmaxthreads (out of workerthreads, out completionportthreads);
Threadpool.getavailablethreads (out of workerthreads, out completionportthreads);
ASP.net has two classes of threads, 1 are worker threads, and the other is IO threads, also called completion port threads. To put it simply, worker threads: threads that handle ordinary requests, the threads that are most commonly used in code.
This thread is finite and is related to the number of root CPUs. IO threads, such as file reading and writing, network operations, and so on can realize the real meaning of performance promotion [asynchronous] asynchronously.
This IO thread, if not specifically handled, is usually not handled, and this IO thread is essentially idle
Is that you can use IO threads instead of worker threads because working threads are limited and valuable for processing user requests.
2threadpool,task These two are actually threads, for ASP.net, the code does not do special processing is usually the worker thread, line Cheng Chili thread
Thread this is the bottom of the threads, without any encapsulation, direct use, creating this thread is more time-consuming and not easy to reuse.
3async/await a new syntactic sugar, a simplified asynchronous programming model, is worth recommending. With this, our asynchronous programming model becomes simple and elegant--this is closely related to the task, how to ... To practice on your own
After understanding the above concepts, we are using best practices to improve performance, throughput
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, it looks similar to other blogs on the web, but such a way for asp.net asynchronous, the effect of lifting throughput is the best, 1th, using IO port, when processing network request [from http://www.asp.net to get data]
The worker thread that was then processed was returned to the thread pool to handle the requests of other users and only one IO thread was occupied 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 ("http://www.asp.net");
}
);
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/
The code doesn't look any different from the code above, but is there an essential difference between the code and the first method above, and does the performance really improve? Is it really possible to increase throughput? Many development is also used in this way
I'll give you the answer here first, this way [using Getarticlecontentbynorigntwayasync] is unlikely to improve performance, especially in asp.net environments
This is true for asynchronous use, as well as task, thread pool. Just used it.
Author: cnblogs Rosanshao