简体   繁体   English

如何同时进行扇出的Akka流处理

[英]How to make fan-out akka-streams processing concurrent

I'm attempting to use Akka Streams to concurrently process a series of dependent streams. 我正在尝试使用Akka流来同时处理一系列依赖流。

Something like this: 像这样:

val concurrency = 2
Source(
  (1 to 5).toStream.map(i => {
    println(s"1: Emitting $i")
    i.toString
  }))
  .mapAsyncUnordered(concurrency)(s => getNextStream(s, 25))
  .mapConcat(identity)
  .mapAsyncUnordered(concurrency)(out => getNextStream(out.x, 50))
  .mapConcat(identity)
  .mapAsyncUnordered(concurrency)(out => getNextStream(out.x, 100))
  .mapConcat(identity)
  .map(x => println(s"4: Received $x after ${System.currentTimeMillis() - start}"))
  .runWith(Sink.ignore)

My problem is it doesn't appear to be running concurrently. 我的问题是它似乎没有同时运行。 Changing the concurrency variable has no effect beyond 2 . 更改concurrency变量对2无效。 I suspect that mapConcat is serializing the processing but I'm not sure. 我怀疑mapConcat正在序列化处理过程,但不确定。

A full, runnable example of the problem can be found here: https://github.com/realrunner/akka-stream-example . 可以在这里找到问题的完整的可运行示例: https : //github.com/realrunner/akka-stream-example

Currently, the code takes 11 seconds to complete. 目前,该代码需要11秒钟才能完成。 I could easily cut it down using raw Actors, without the benefit of properly handling backpressure. 我可以使用原始Actor轻松将其削减,而无需适当处理背压。 Any ideas on how to make this more concurrent? 关于如何使其更加并发的任何想法?

Each and every one of these getNextStream calls go out and use ask against one single(ton) actor. 这些getNextStream调用中的每一个都出去,对单个(ton)演员使用ask Keep in mind an actor processes incoming messages in a serialized manner. 请记住,参与者以串行方式处理传入的消息。

Now when processing the message, you block the actor by using Thread.sleep . 现在,在处理消息时,您可以使用Thread.sleep阻止actor。 Blocking is generally discouraged within Akka - see this bit of the docs . 通常在Akka中不建议使用阻塞-请参阅文档的这一部分。

Depending on what is the real behaviour of your actors, you can simulate long processing in a non-blocking way using the after pattern (see docs ), or if blocking is really needed - but double, triple check that - you can block on a dedicated dispatcher (as explained here ). 根据角色的实际行为,您可以使用after模式(请参阅docs )以非阻塞方式模拟长时间处理,或者是否确实需要阻塞-但要进行两次,三次检查-您可以阻塞专用调度(如解释在这里 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM