Akka Source 不流数据?
Akka Source not streaming data?
val pageDataFutures : Seq[Future[PageData]]= ??? //4 api calls each resulting in a future of PageData
def source : Source[PageData, NotUsed] = Source(
pageDataFutures.flatMap(future => Await.result(future,atMost)).toList
)
source.runForeach(println)
我希望在每个 future 完成时 运行 'runForEach'
来源。但是所有 4 api 调用都被调用,然后源中的数据被一次打印出来。它不应该打印可用的数据吗?我为每个未来使用 Await
。因此可以保证在下一个 future Awaited
之前,前一个 future 的结果可用并且可以被 println
使用
使用mapAsync
:
Pass incoming elements to a function that return a Future
result. When the Future
arrives the result is passed downstream. Up to n
elements can be processed concurrently, but regardless of their completion time the incoming order will be kept when results complete.
val pageDataFutures: Seq[Future[PageData]] = ???
Source(pageDataFutures)
.mapAsync(parallelism = 1)(x => x) // or: mapAsync(parallelism = 1)(identity)
.runForeach(println)
val pageDataFutures : Seq[Future[PageData]]= ??? //4 api calls each resulting in a future of PageData
def source : Source[PageData, NotUsed] = Source(
pageDataFutures.flatMap(future => Await.result(future,atMost)).toList
)
source.runForeach(println)
我希望在每个 future 完成时 运行 'runForEach'
来源。但是所有 4 api 调用都被调用,然后源中的数据被一次打印出来。它不应该打印可用的数据吗?我为每个未来使用 Await
。因此可以保证在下一个 future Awaited
之前,前一个 future 的结果可用并且可以被 println
使用mapAsync
:
Pass incoming elements to a function that return a
Future
result. When theFuture
arrives the result is passed downstream. Up ton
elements can be processed concurrently, but regardless of their completion time the incoming order will be kept when results complete.
val pageDataFutures: Seq[Future[PageData]] = ???
Source(pageDataFutures)
.mapAsync(parallelism = 1)(x => x) // or: mapAsync(parallelism = 1)(identity)
.runForeach(println)