异步/等待模式是否会对次要工作单元造成性能损失? [英] Can async/await pattern cause performance penalties on minor units of work?

查看:58
本文介绍了异步/等待模式是否会对次要工作单元造成性能损失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找关于工作负载阈值"的一些指导,这些指导在异步/等待有意义的地方(例如释放IO完成端口并避免线程匮乏)与工作单元过于简单/廉价以致无法执行,因此同步执行是一个更好的选择.

I'm looking for some guidance on the "workload threshold" between where async/await makes sense (e.g. to free up IO Completion Ports and avoid thread starvation) vs. unit of work being too simple/cheap to execute, so synchronous execution is a better choice.

换句话说,当与相对快速/资源廉价的工作单元结合使用时,异步/等待的使用是否会导致性能下降,而仅同步执行工作将是首选方法?

In other words, could the use of async/await lead to degraded performance when used in conjunction with relatively fast/resource-inexpensive units of work, and simply executing the work synchronously would be the preferred approach?

示例(一种方法,全部与await异步):

Example (all in one method, all asynchronous with await):

  • 将一些次要更新保存到数据库
  • 使用ReadAsStreamAsync
  • 将很小的文件上传到流
  • 使用CopyToAsync
  • 将读取的流复制到文件流
  • 使用FlushAsync
  • 刷新编写器流
  • Save some minor update to DB
  • Reading a very small file upload to a stream using ReadAsStreamAsync
  • Copy read stream to file stream using CopyToAsync
  • Flushing writer stream using FlushAsync

推荐答案

我建议您不要从计时角度来看,而是从I/O-vs-CPU角度来看.

I recommend you approach this not from a timing standpoint, but from an I/O-vs-CPU standpoint.

与CPU绑定的方法自然是同步的. I/O绑定方法自然是异步的.

CPU-bound methods are naturally synchronous; I/O-bound methods are naturally asynchronous.

根据您的示例,我假设您的环境是服务器端的

I'm assuming that your environment is server-side, based on your example:

  • 将一些次要更新保存到数据库.自然地是异步的:网络(或至少是进程外)通信,争用的可能性,磁盘I/O.
  • 使用ReadAsStreamAsync 将很小的文件上传到流中.自然地是异步的:网络通信.
  • 使用CopyToAsync 将读取的流复制到文件流.自然地异步:争用的可能性,磁盘I/O.
  • 使用FlushAsync刷新记录编写器流.自然地异步:争用的可能性,磁盘I/O.
  • Save some minor update to DB. Naturally asynchronous: network (or at least out-of-proc) communication, possibility of contention, disk I/O.
  • Reading a very small file upload to a stream using ReadAsStreamAsync. Naturally asynchronous: network communication.
  • Copy read stream to file stream using CopyToAsync. Naturally asynchronous: possibility of contention, disk I/O.
  • Flushing writer stream using FlushAsync. Naturally asynchronous: possibility of contention, disk I/O.

所有这些都是自然异步操作,因此应该全部异步实现. CPU比内存快得多,而内存比网络或磁盘I/O快得多.因此,如果您同步实现自然的异步方法,则阻塞线程.这不是世界末日,但是如果线程池被阻塞的时间太长,则必须对其进行补偿,并且唯一节省"的时间就是线程切换时间,该时间将缩短几个数量级. .

All of these are naturally-asynchronous operations, so they should all be implemented asynchronously. CPUs are incredibly faster than memory, which is incredibly faster than network or disk I/O. Because of this, if you implement a naturally asynchronous method synchronously, you will block a thread. This isn't the end of the world, but the thread pool has to compensate if it's blocked for too long, and the only time you're "saving" is the thread switch time, which will be orders of magnitude shorter than any network or disk I/O could possibly be.

要考虑的另一件事是不可预测的延迟,通常由于资源争用而发生.如果另一个进程同时写入数据库怎么办?如果文件上传时出现路由故障,需要重新传输数据包怎么办?如果在尝试写入输出文件时磁盘出现碎片整理该怎么办?异步操作具有这种不可预测的趋势,并且async代码可确保您阻塞线程的时间不会比预期的长.

Another thing to consider is unpredictable latency, which usually happens due to resource contention. What if another process is writing to the db at the same time? What if there's a routing hiccup while the file is uploading, requiring retransmission of packets? What if the disk is defragmenting while you're trying to write the output file? Asynchronous operations have a tendency to be unpredictable like this, and async code ensures you won't be blocking a thread for much longer than you expected.

总结:将同步代码用于同步(与CPU绑定)工作,而异步代码用于异步(与I/O绑定)工作.

In conclusion: use synchronous code for synchronous (CPU-bound) work, and asynchronous code for asynchronous (I/O-bound) work.

这篇关于异步/等待模式是否会对次要工作单元造成性能损失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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