【发布时间】:2018-04-28 07:42:39
【问题描述】:
我试图了解asyncBoundary 和mapAsync 之间的区别。乍一看,我想它们应该是一样的。但是,当我运行代码时,看起来asyncBoundary 的性能比mapAsync 快
这里是代码
implicit val system = ActorSystem("sourceDemo")
implicit val materializer = ActorMaterializer()
Source(1 to 100).mapAsync(100)(t => Future {t + 1}).mapAsync(100)(t => Future {t * 2}).map(println).to(Sink.ignore).run()
Source(1 to 100).map(_ + 1).withAttributes(Attributes.asyncBoundary).map(_ * 2).map(t => println("async boundary", t)).to(Sink.ignore).run()
输出: 异步边界总是比 mayAsync 更快地完成。
从关于 asyncBoundary (https://doc.akka.io/docs/akka-stream-and-http-experimental/current/scala/stream-flows-and-basics.html) 的文档中,我可以看到它在不同的 CPU 上运行,但 mapAsync 是使用 Future 实现的多线程。 Future 也是异步的。
我可以要求更多关于这两个 API 的说明吗?
【问题讨论】:
标签: scala akka akka-stream stream-processing