Akka Streams:调用push(_,_)不在onPull(_,_)内阻塞流 - 为什么?

时间:2018-03-22 21:56:18

标签: scala akka akka-stream

我无法理解小样本客户Akka Streams Source的行为。

示例背后的想法是Source应该向Actor请求下一个元素。请参阅以下代码

shared_lstm1 <- layer_lstm(units = 64, return_sequences = TRUE)
shared_lstm2 <- layer_lstm(units = 64, return_sequences = TRUE)
shared_lstm3 <- layer_lstm(units = 64)

encoded_a <- tweet_a %>% shared_lstm1 %>% shared_lstm2 %>% shared_lstm3
encoded_b <- tweet_b %>% shared_lstm1 %>% shared_lstm2 %>% shared_lstm3

如果您创建一个包含该源的流:

class ActorSource[T](context: ActorRefFactory, actor: ActorRef) extends GraphStage[SourceShape[T]] {

  val out: Outlet[T] = Outlet("actor-source")

  override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = {
    new GraphStageLogic(shape) {
      setHandler(out, new OutHandler {
        val receivingActor = context.actorOf(Props(new ReceivingActor(msg => {
          push(out, msg)
          println("push - Done")
        })))

        override def onPull(): Unit = {
          actor ! Protocol.Pull(receivingActor)
          println("onPull - Done")
        }
      })
    }
  }

  override def shape: SourceShape[T] = SourceShape(out)

  /**
    * A small actor which receives new elements from the actual source actor.
    *
    * @param push The method to push elements into the stream
    */
  class ReceivingActor(push: T => Unit) extends Actor with ActorLogging with UnknownMessage {

    override def receive: Receive = {
      case Protocol.Push(msg) =>
        push(msg.asInstanceOf[T])  // I know that this is evil ....just for test in that case...
      case msg =>
        unknownMessage(msg)
    }

  }

}

object ActorSource {

  /**
    * Creates an [[ActorSource]]
    *
    * @param actor   The actor which acts as the source
    * @param context The context to create the internal helper actor
    * @return A new akka-streams source
    */
  def Source[T](actor: ActorRef)(implicit context: ActorRefFactory): AkkaSource[T, NotUsed] = {
    val graph: Graph[SourceShape[T], NotUsed] = new ActorSource[T](context, actor)
    AkkaSource.fromGraph(graph)
  }

  /**
    * Defines the messages/ events for the source actor
    */
  object Protocol {

    /**
      * Will be sent if the stream requires new elements.
      *
      * @param actor The actor which should receive the push message
      */
    case class Pull(actor: ActorRef)

    /**
      * Sent by the source actor to submit a new element.
      *
      * @param msg The message to put into the stream.
      */
    case class Push(msg: Any)

  }

}

输出仅如下:

class SampleActor extends Actor with ActorLogging with UnknownMessage {

  var counter = 0

  override def receive: Receive = {
    case msg @ Protocol.Pull(actor) =>
      actor ! Protocol.Push(counter)
      counter = counter + 1
  }

}

val sourceActor = system.actorOf(Props(new SampleActor()))

val stream = ActorSource
  .Source[Int](sourceActor)(system)
  .take(10)
  .runWith(Sink.foreach(println))

Await.result(stream, 30 seconds)

第一个整数永远不会到达接收器,并且不再调用onPull - Done push - Done 。有趣的是,如果我终止程序,第一个整数将被打印在接收器中。

我想知道这是一个功能还是一个bug?根据我的理解,可以在一个onPull表示插座已打开后的任何时候拨打push(_, _),即使我要求pull,它也会返回isAvailable

任何人都可以解释这种行为吗?

1 个答案:

答案 0 :(得分:0)

在阅读了更多文档(RTFM;)后,我想我找到了问题的答案。

在多个地方提到,在相关的回调之外调用这些API方法是不安全的。为了实现我要实现的目标,Akka Streams提供了getAsyncCallback方法。

更多细节可以在这里找到:https://doc.akka.io/docs/akka/2.5.4/scala/stream/stream-customize.html#using-asynchronous-side-channels