run()和runWIth()之间的标量-akka-数据流差异

pdtvr36n  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(92)

run和runWith的主要区别是什么:-

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Keep, Sink, Source}

object RunAndRunWith extends App {

  implicit val system: ActorSystem = ActorSystem("Run_RunWith")
  implicit val materializer: ActorMaterializer = ActorMaterializer()

  Source(1 to 10).toMat(Sink.foreach[Int](println))(Keep.right).run()
  Source(1 to 10).runWith(Sink.foreach[Int](println))

}

如何知道使用哪一个?

q3qa4bjr

q3qa4bjr1#

to(Sink)toMat(Sink)用sink终止源代码,并产生一个RunnableGraph,您可以使用run()执行它,但它也让您有机会在运行它之前为整个图设置流属性,或者将它交给其他函数/方法来运行它(或者可能对它执行其他操作)。
如果需要,此表单还可以让您控制物化值的来源。
由于想要终止并运行一个带有接收器的源,而不需要任何附加属性,并保持接收器的物化值是如此常见,因此runWith(Sink)是一个方便的快捷方式。

相关问题