异步等待使用和开销 [英] Async await usage and overhead

查看:104
本文介绍了异步等待使用和开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与服务类对话的Web api 2端点. 服务类调用Entity Framework(v6)

I have a web api 2 endpoint that talks to a service class. The service class makes calls to Entity Framework (v6)

我已在使用SaveChangesAsync()等方法将异步写入数据库,查询等方法上应用了异步等待.

I have applied async await on the methods where im writing to the database, querying etc.. using SaveChangesAsync(), etc

我的问题与开销有关.

我必须在服务中的方法签名上应用异步的事实意味着,然后我必须在任何调用方法上应用await和async才能进行编译.

the fact i have to apply async on the method signature in the service means i then have to apply await and async on any calling method in order to compile.

事实是我撒了异步&等待调用服务方法的方法-这会导致很多额外的开销吗?即:我的Web api 2端点现在已对其应用异步等待,因为我正在调用具有异步&任务也适用于它的签名.

The fact im sprinkling async & await on methods calling the service method - does this result in a lot of extra overhead? i.e.: my web api 2 endpoint now has async await applied to it as im calling this service method that has async & Task applied on it signature too.

我谈论的管理费用与本文有关 https://www .simple-talk.com/dotnet/.net-framework/the-overhead-of-asyncawait-in-net-4.5/

The overhead i talk about is related to this article https://www.simple-talk.com/dotnet/.net-framework/the-overhead-of-asyncawait-in-net-4.5/

推荐答案

是的,这会导致一些开销.通常,这不是问题.如果您期望每秒少于1M的任务,那么我根本不会考虑这一点.

It results in some overhead, yes. Normally, it is not a concern. If you expect less than 1M tasks per second I would not think about this at all.

使用一路异步"的最大代价是,它会感染大量代码并影响代码质量.由于它更容易出错,因此会增加开发成本并降低软件质量.

The biggest cost of using "async all the way" is that it infects a lot of code and impacts code quality. It can increase development cost and decrease software quality because it tends to be more error prone.

这篇关于异步等待使用和开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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