A new technology or a feature that only you can use to solve practical problems, to truly appreciate its charm, to really understand it. We also look forward to sharing some solutions to practical problems.
In the process of encountering the "Black 30 second" problem, we experienced the great effect of asynchrony (see the new analysis of "black 30 seconds" from the asp.net thread angle), and began to transform the existing code gradually with async/await.
This morning, when the action in an MVC controller was changed to asynchronous, it was suddenly discovered that 7 of the method calls could be executed in parallel.
Public Async task<actionresult> Blogpostinfo (string blogapp, int blogId, int postid, Guid bloguserguid)
{
7 methods are not associated with a method call
}
If the 7 methods are implemented in parallel through async/await, the performance will be improved several times, it is an unexpected surprise!
After the surprise, you have to face the problem-how to achieve it at the lowest cost?
These 7 methods are also called elsewhere, and do not want to change these methods directly to asynchronous, even if you can change to asynchronous, and do not want to go all the way to the end, finally call the Ado.net asynchronous method at the data access layer.
。。。
And then found another surprise in the garden.--jesse Liu's blog (Async & await's previous life) is a picture of:
What a beautiful picture! Even the order of execution is clearly marked. Just follow this picture, you can easily use async/await to achieve parallelism.
Places to look for:
1) The target method of parallel invocation must be async.
2 in parallel, you cannot use await.
Here is the implementation case:
The following code is 2 of the 7 methods that need to be executed in parallel:
var tags = tagservice.gettag (blogId, PostID);
if (!string. IsNullOrEmpty (tags))
{
info. Tags = string. Format ("tags: {0}", Tagservice.gettaglink (Blogurl, tags));
}
var categories = Categoryservice.getcatelist (Blogurl, BlogId, PostID);
if (!string. IsNullOrEmpty (categories))
{
info. Categories = "Category:" + Categories;
}
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/
Since the target method of the parallel invocation must be async, and we do not want to modify the original method implementation code, we add 2 async methods to the relay:
Async Method 1:
public static async task<string> gettagasync (int blogId, int entryId)
{return
await Task.run (() => {R Eturn Gettag (BlogId, entryId); });
}
Async Method 2:
public static Async String Getcatelistasnyc (string blogurl, int blogId, int entryId)
{return
await Task.run () => {return getcatelist (Blogurl, BlogId, entryId);});
In the calling code, the 2 async methods are called to execute in parallel, and then the execution results are await.
var tagstask = Tagservice.gettagasync (blogId, PostID);
var categoriestask = Blogcategoryservice.getcatelistasync (Blogurl, BlogId, PostID);
var tags = await tagstask;
if (!string. IsNullOrEmpty (tags))
{
info. Tags = string. Format ("tags: {0}", Tagservice.gettaglink (null, Blogurl, tags));
}
var categories = await categoriestask;
if (!string. IsNullOrEmpty ((categories))
{
info. Categories = "Category:" + Categories;
}
It's really simple, it's easy! Async/await really good!