繼documentation to implement a KillSwitch
之後,我能夠編寫這個簡單的示例來停止Source發出無限數字。Akka Streams:KillSwitch自定義SourceShape從視頻文件中發射幀
object KillSwitchSample extends App {
implicit val actorSystem = ActorSystem()
implicit val materializer = ActorMaterializer()
val sourceGraph: Graph[SourceShape[Int], NotUsed] = new NumbersSource
val mySource: Source[Int, NotUsed] = Source.fromGraph(sourceGraph)
val killSwitch = KillSwitches.shared("switch")
RunnableGraph.fromGraph(GraphDSL.create() { implicit builder =>
import GraphDSL.Implicits._
val flow = builder.add(Flow[Int].map(_ * 2))
mySource.via(killSwitch.flow) ~> flow ~> Sink.foreach(println)
ClosedShape
}).run()
Thread.sleep(200)
killSwitch.shutdown()
}
class NumbersSource extends GraphStage[SourceShape[Int]] {
val out: Outlet[Int] = Outlet("NumbersSource")
override val shape: SourceShape[Int] = SourceShape(out)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
new GraphStageLogic(shape) {
private var counter = 1
setHandler(out, new OutHandler {
override def onPull(): Unit = {
push(out, counter)
counter += 1
}
})
}
}
我的使用情況是,該源發射使用OpenCV的視頻文件幀的意義不同。爲什麼上游沒有取消?我在這裏錯過了什麼?
object KillSwitchMinimalMain extends App {
val libopencv_java = new File("lib").listFiles().map(_.getAbsolutePath).filter(_.contains("libopencv_java"))
System.load(libopencv_java(0))
implicit val actorSystem = ActorSystem()
implicit val materializer = ActorMaterializer()
val videoFile = Video("Video.MOV")
val sourceGraph: Graph[SourceShape[Frame], NotUsed] = new VideoSource(videoFile)
val videoSource: Source[Frame, NotUsed] = Source.fromGraph(sourceGraph)
val killSwitch = KillSwitches.shared("switch")
RunnableGraph.fromGraph(GraphDSL.create() { implicit builder =>
import GraphDSL.Implicits._
val matConversion: FlowShape[Frame, Image] = builder.add(Flow[Frame].map { el => MediaConversion.convertMatToImage(el.frame) })
videoSource.via(killSwitch.flow) ~> matConversion ~> Sink.foreach(println)
ClosedShape
}).run()
Thread.sleep(200)
killSwitch.shutdown()
}
class VideoSource(videoFile: Video) extends GraphStage[SourceShape[Frame]] {
val out: Outlet[Frame] = Outlet("VideoSource")
override val shape: SourceShape[Frame] = SourceShape(out)
val log: Logger = LoggerFactory.getLogger(getClass)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
new GraphStageLogic(shape) {
private val capture = new VideoCapture()
private val frame = new Mat()
private var videoPos: Double = _
override def preStart(): Unit = {
capture.open(videoFile.filepath)
readFrame()
}
setHandler(out, new OutHandler {
override def onPull(): Unit = {
push(out, Frame(videoPos, frame))
readFrame()
}
})
private def readFrame(): Unit = {
if (capture.isOpened) {
videoPos = capture.get(1)
log.info(s"reading frame $videoPos")
capture.read(frame)
}
}
}
}
控制檯輸出要求通過@svezfaz:
13:17:00.046 [default-akka.actor.default-dispatcher-3] INFO de.itd.video.VideoSource - reading frame 0.0
13:17:00.160 [default-akka.actor.default-dispatcher-3] INFO de.itd.video.VideoSource - reading frame 1.0
[email protected]
13:17:00.698 [default-akka.actor.default-dispatcher-3] INFO de.itd.video.VideoSource - reading frame 2.0
[email protected]
13:17:00.826 [default-akka.actor.default-dispatcher-3] INFO de.itd.video.VideoSource - reading frame 3.0
[email protected]
13:17:00.969 [default-akka.actor.default-dispatcher-3] INFO de.itd.video.VideoSource - reading frame 4.0
[email protected]
13:17:01.137 [default-akka.actor.default-dispatcher-3] INFO de.itd.video.VideoSource - reading frame 5.0
[email protected]
// and so on ..
您能詳細介紹一下您在運行OpenCV示例時看到的輸出內容嗎?你知道一幀拉多少時間? –
我用一個記錄器更新了這個問題,表明幀的讀取速度相當快(當然比發射整數慢得多)。 'javafx.scene.image.WritableImage @ xxxxxxxx'是接收器的'println'。 – Toaditoad