当我应该使用ASP.NET MVC异步控制器? [英] When should I use Async Controllers in ASP.NET MVC?

查看:189
本文介绍了当我应该使用ASP.NET MVC异步控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC的异步行为的担忧。当它提高了我的应用程序的性能,并执行时,它的的?


  1. 这是好到ASP.NET MVC?
  2. 到处使用异步操作
  3. 关于awaitable方法:我将使用异步/等待的关键字时,我想查询数据库(通过EF / NHibernate的/其他的ORM)

  4. 多少次我可以使用关键字伺机异步查询数据库中的有一个的单个动作的方法?


解决方案

异步动作方法是有用的,当一个动作必须执行几个独立的长时间运行的操作。

假设我们有三个操作这需要的 500,600和700 毫秒。与同步调用,总响应时间会比1800毫秒略多。然而,如果呼叫被异步制成(并行),总反应时间将是超过700毫秒略多,因为那是最长的任务/操作的持续时间。


  

对于AsyncController类的典型用途是长期运行的Web
  服务电话。


如果我的数据库调用是异步的?

在IIS线程池通常可以处理比一台数据库服务器更多的并发阻塞请求。如果数据库是瓶颈,异步调用不会加快数据库的响应。未经节流机构,通过使用异步调用有效调度更多的工作,以一个不知所措数据库服务器仅仅转移更多的负担到数据库。如果你的数据库是瓶颈,异步调用不会是灵丹妙药。

看看<一个href=\"http://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx\">1和 2 引用

从@PanagiotisKanavos衍生评论:


  

此外,异步并不意味着平行。异步执行将释放
  从阻断外部资源,有价值的线程池线程
  没有复杂性或性能开销。这意味着相同的IIS机器可以
  处理更多并发请求,而不是它会运行得更快。


  
  

您还应该考虑到,阻塞调用先从
  CPU密集型spinwait。在紧张时期,阻塞调用会
  导致升级的延误和应用程序池回收。异步调用
  简单地避免这种


I have some concerns using async actions in ASP.NET MVC. When does it improve performance of my apps, and when does it not?

  1. Is it good to use async action everywhere in ASP.NET MVC?
  2. Regarding awaitable methods: shall I use async/await keywords when I want to query a database (via EF/NHibernate/other ORM)?
  3. How many times can I use await keywords to query the database asynchronously in one single action method?

解决方案

Asynchronous action methods are useful when an action must perform several independent long running operations.

Suppose we have three operations which takes 500, 600 and 700 milliseconds. With the synchronous call, total response time would be slightly more than 1800 milliseconds. However, if the calls are made asynchronously (in parallel), total response time would be slightly more than 700 milliseconds, because that is the duration of longest task/operation.

A typical use for the AsyncController class is long-running Web service calls.

Should my database calls be asynchronous ?

The IIS thread pool can often handle many more simultaneous blocking requests than a database server. If the database is the bottleneck, asynchronous calls will not speed up the database response. Without a throttling mechanism, efficiently dispatching more work to an overwhelmed database server by using asynchronous calls merely shifts more of the burden to the database. If your DB is the bottleneck, asynchronous calls won’t be the magic bullet.

You should have a look at 1 and 2 references

Derived from @PanagiotisKanavos comments:

Moreover, async doesn't mean parallel. Asynchronous execution frees a valuable threadpool thread from blocking for an external resource, for no complexity or performance cost. This means the same IIS machine can handle more concurrent requests, not that it will run faster.

You should also consider that blocking calls start with a CPU-intensive spinwait. During stress times, blocking calls will result in escalating delays and app pool recycling. Asynchronous calls simply avoid this

这篇关于当我应该使用ASP.NET MVC异步控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆