为什么 EF 6 教程使用异步调用? [英] Why does the EF 6 tutorial use asynchronous calls?

查看:38
本文介绍了为什么 EF 6 教程使用异步调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

介绍如何在 MVC 5 中使用 EF 6 的最新 EF 教程似乎倾向于使用异步调用数据库,例如:

The latest EF tutorial that goes through how to use EF 6 with MVC 5 seems to lean towards using asych calls to the database like:

Department department = await db.Departments.FindAsync(id);

这是新标准/最佳实践吗?

Is this the new standard/best practice?

我不确定这种使用 ASP.NET MVC 的开发风格有什么好处.

I'm not sure what the benefit is for this style of development with ASP.NET MVC.

有人可以评论一下这个模式,这是MS正在推广的新标准吗?

Can someone comment on this pattern, is this the new standard that MS is promoting?

推荐答案

为了决定是去异步还是同步,比较收益和成本:

In order to decide whether to go async or sync, compare the benefits and costs:

异步:

  • 几乎从不使用异步耗尽线程池(情况必须非常极端)
  • 几乎任意级别的并发(并发请求和操作)
  • 每个线程节省 1MB 内存
  • 借助 SynchronizationContext
  • 实现安全的请求内并发
  • 由于减少了操作系统调度开销,可以在高负载情况下以较低的两位数百分比提高吞吐量.也就是说,几乎没有生产应用处于高 CPU 负载下,因为如果它接近不可用(在负载高峰的情况下应用开始丢弃请求)

同步:

  • 更简单的代码:await 使 99% 的情况(几乎)像同步代码一样简单.也就是说,Stack Overflow 上每天有 10 多个异步问题使用不同的语言.当您偏离简单路径时,就会出现边缘情况.此外,在使用旧库时,例如,需要您向它们传递同步回调
  • 减少编码和调试工作
  • 分析器友好(您可以分析应用程序或只是暂停调试器并查看应用程序现在正在做什么.异步无法实现.)
  • 与遗留代码和库完美互操作

如果您要调用高延迟服务,请选择与 ASP.NET 异步. Web 服务可能具有高延迟.OLTP 数据库几乎总是低延迟的.

Choose async with ASP.NET if you are calling high-latency services. A web-service is likely to be high latency. An OLTP database is almost always low-latency.

如果您的应用程序受益于非常高的并发级别(100+),请选择异步.大多数应用程序没有这么高的级别,或者它们的后端服务无法承受这么大的负载.没有必要让 Web 应用程序扩展,而是让后端过载.调用链中的所有系统必须受益于高度的并发性,以便异步受益.

Choose async if your application benefits from very high levels of concurrency (100+). Most applications do not have such high levels, or their back-end services would not sustain such an amount of load. No point in making the web app scale but overload the back-end. All systems in the call chain must benefit from a high degree of concurrency in order for async to be beneficial.

典型的高延迟服务(异步的好例子):

  • 网络服务
  • 等待(例如睡眠)
  • 节流 (SemaphoreSlim, ...)
  • 一些云服务(Azure)
  • 对数据库的长时间查询(例如报告或 ETL)

典型的低延迟服务(同步的好案例):

  • 数据库调用:大多数 OLTP 查询都是低延迟的,因为您可以假设数据库服务器不会过载.向它抛出 100 个并发查询是没有意义的.不会使它们完成得更快.
  • 文件系统:与数据库相同.

这些按典型案例分类.所有这些也可能属于相反的类别.

These are categorized by the typical case. All of these can be in the opposite category as well.

您可以在同一个应用中混合使用同步和异步.当异步处于最佳状态时使用异步.

You can mix sync and async in the same app. Use async when it is at its sweet spot.

那么为什么微软和实体框架团队要推广异步使用呢?这是这个答案的主观部分:这可能是微软的内部政策.他们可能会预期客户端应用程序中的 EF 使用情况(异步非常适合).或者,他们没有意识到异步数据库调用几乎总是浪费开发人员的时间而没有任何好处.大多数人没有意识到这一点,因为现在异步是要走的路.

So why are Microsoft and the Entity Framework team promoting async usage? Here comes the subjective part of this answer: It might be Microsoft internal policy. They might anticipate EF usage in client apps (for which async is great). Or, they don't realize that async database calls are pretty much almost always a waste of developer's time without benefits. Most people don't realize this because async is the way to go these days.

这篇关于为什么 EF 6 教程使用异步调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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