MVC应该一切都是异步的 [英] mvc should everything be async

查看:62
本文介绍了MVC应该一切都是异步的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前正在构建MVC 5应用程序,该应用程序调用业务层并调用数据库,没有WCF或Web Api服务.目前我们所有的控制器都不是异步的,我一直在研究任务/异步,并想知道使控制器中的所有动作异步是否变得普遍吗?对我们来说,所有操作都要求业务层通过Entity Framework读取数据,因此将它们全部更改为asnyc是否有意义?还是仅在中间层(例如WCF或Web API服务)可能涉及值得的网络延迟时才真正有用?

We currently are building an MVC 5 application, the application makes calls to a Business Layer which calls the database, there is no WCF or Web Api service. All of our controllers are currently not async, i've been looking into task/async and wanted to know if it's becoming common to make all Actions in a Controller async? For us, all actions are requesting the business layer to read data via Entity Framework, so would it make sense to change them all to asnyc? Or it is really only helpful when there is a middle layer, like a WCF or Web API service which might involve network latency that it's worth it?

推荐答案

所有异步所做的就是允许将处于等待状态的线程返回到线程池以进行其他操作.当原始线程正在等待的任何操作完成时,都会从线程池中请求一个线程来完成总体任务.由于线程必须处于等待状态,因此某些操作(例如通过HTTP向API发出请求)是异步的理想选择,而其他操作(例如复杂的计算和其他受CPU限制的工作)则完全不适合异步操作.

All async does is allow a thread that is in a wait-state be returned to the thread pool to field other operations. When whatever operation the original thread was waiting on finishes, a thread is requested back from the thread pool to finish the overarching task. Because the thread must be waiting, certain operations like making a request over HTTP to an API are ideal candidates for async while other operations like complex calculations and other CPU-bound work are completely inappropriate for async.

现在,由于您的问题与数据库查询有关,所以答案取决于它.例如,如果您的数据库恰好与您的网站位于同一台服务器上(顺便说一句,这绝对不是一个好主意),异步对您没有多大帮助.可能要等待一些时间,但是要微乎其微,以至于传递线程要比保持线程直到数据库完成工作要花费更长的时间.现在,如果您做的正确,并且数据库位于远程服务器上,则网络延迟开始起作用.内部网络上的数据库可能仍然不能从异步中获得很多好处,因为延迟可能仍然很低,但是异步仍然可以潜在地帮助一些人.但是,如果您的数据库位于外部网络上,则绝对可以保证异步,因为延迟会高得多.

Now, since your question is regarding database queries, the answer is it depends. For example, if your database happened to be on the same server as your website (which is totally not a good idea BTW) async wouldn't do much for you. There may be some waiting, but it's going to be miniscule to the point where it would probably take longer to pass the threads around than it would to just hold the thread until the database finishes its work. Now, if you're doing things right and your database is on a remote server, network latency starts to come into play. A database on an internal network may still not benefit much from async, since the latency would still probably be very low, but async could still potentially help some. If your database is on an external network, though, then async would definitely be warranted as latency would be much higher.

不过,使用异步可能比不使用更好.即使等待时间很短,也并非总是如此.而且,如果您的网络受到重创,将所有内容设置为异步将使您的Web服务器有一些宝贵的回旋余地来处理现场请求,否则它们可能会在负载下死锁.

Still, it's probably better err on the side of using async than not. Even if latency is low, that may not always be the case. And if your network is getting hammered, having everything set up as async will give your web server some valuable leeway to field requests where it otherwise might deadlock under the load.

这篇关于MVC应该一切都是异步的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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