Akka Streams - 了解物化何时以及如何工作
Akka Streams - Understanding when and how materialisation works
我正在开发的一个应用 requires/gives 用户能够在 运行 时创建和定义任意流。我了解特别是在 Akka 流中
具体化=执行或运行
我的问题
1) 流的物化是否应该只进行一次?即如果它已经具体化那么我可以使用后续 运行s 的值吗?
2) 如上所述,可能我误解了物化这个词。如果流必须 运行,它每次都具体化?
我很困惑,因为在文档中,它说物化实际上创建了流执行所需的资源。所以我的直接理解是它只能做一次。就像与数据库的 JDBC 连接一样。有人可以用非 akka 术语解释一下吗?
是的,一个流可以实现多次。是的,如果一个流 运行 多次,它每次都会具体化。来自 documentation:
Since a stream can be materialized multiple times, the materialized value will also be calculated anew for each such materialization, usually leading to different values being returned each time. In the example below we create two running materialized instance of the stream that we described in the runnable
variable, and both materializations give us a different Future
from the map even though we used the same sink
to refer to the future:
// connect the Source to the Sink, obtaining a RunnableGraph
val sink = Sink.fold[Int, Int](0)(_ + _)
val runnable: RunnableGraph[Future[Int]] =
Source(1 to 10).toMat(sink)(Keep.right)
// get the materialized value of the FoldSink
val sum1: Future[Int] = runnable.run()
val sum2: Future[Int] = runnable.run()
// sum1 and sum2 are different Futures!
将流视为可以 run/materialized 多次的可重用蓝图。实体化一个流需要一个实体化器,Akka Streams 提供了一个实体化器,叫做ActorMaterializer
。物化器分配必要的资源(参与者等)并执行流。虽然对不同的流和多个实体化使用相同的实体化器很常见,但流的每个实体化都会触发 运行 流所需的资源分配。在上面的示例中,sum1
和 sum2
使用相同的蓝图 (runnable
) 和相同的物化器,但它们是导致不同资源分配的不同物化的结果。
我正在开发的一个应用 requires/gives 用户能够在 运行 时创建和定义任意流。我了解特别是在 Akka 流中
具体化=执行或运行
我的问题
1) 流的物化是否应该只进行一次?即如果它已经具体化那么我可以使用后续 运行s 的值吗?
2) 如上所述,可能我误解了物化这个词。如果流必须 运行,它每次都具体化?
我很困惑,因为在文档中,它说物化实际上创建了流执行所需的资源。所以我的直接理解是它只能做一次。就像与数据库的 JDBC 连接一样。有人可以用非 akka 术语解释一下吗?
是的,一个流可以实现多次。是的,如果一个流 运行 多次,它每次都会具体化。来自 documentation:
Since a stream can be materialized multiple times, the materialized value will also be calculated anew for each such materialization, usually leading to different values being returned each time. In the example below we create two running materialized instance of the stream that we described in the
runnable
variable, and both materializations give us a differentFuture
from the map even though we used the samesink
to refer to the future:// connect the Source to the Sink, obtaining a RunnableGraph val sink = Sink.fold[Int, Int](0)(_ + _) val runnable: RunnableGraph[Future[Int]] = Source(1 to 10).toMat(sink)(Keep.right) // get the materialized value of the FoldSink val sum1: Future[Int] = runnable.run() val sum2: Future[Int] = runnable.run() // sum1 and sum2 are different Futures!
将流视为可以 run/materialized 多次的可重用蓝图。实体化一个流需要一个实体化器,Akka Streams 提供了一个实体化器,叫做ActorMaterializer
。物化器分配必要的资源(参与者等)并执行流。虽然对不同的流和多个实体化使用相同的实体化器很常见,但流的每个实体化都会触发 运行 流所需的资源分配。在上面的示例中,sum1
和 sum2
使用相同的蓝图 (runnable
) 和相同的物化器,但它们是导致不同资源分配的不同物化的结果。