我试图理解 和 之间的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 的说明吗?