假设我有一个句子逐行处理的文件。就我而言,我需要从这些行中提取命名实体(人员,组织......)。不幸的是,标记器很慢。因此,我决定并行化计算,以便可以相互独立地处理行,并将结果收集在中心位置。
我目前的方法包括使用单个生产者多个消费者概念。但是,我对Akka很新,但我认为我的问题描述很适合它的功能。让我给你看一些代码:
Producer
逐行读取文件并将其发送到Consumer
。如果它达到总行限制,它会将结果传播回WordCount
。
class Producer(consumers: ActorRef) extends Actor with ActorLogging {
var master: Option[ActorRef] = None
var result = immutable.List[String]()
var totalLines = 0
var linesProcessed = 0
override def receive = {
case StartProcessing() => {
master = Some(sender)
Source.fromFile("sent.txt", "utf-8").getLines.foreach { line =>
consumers ! Sentence(line)
totalLines += 1
}
context.stop(self)
}
case SentenceProcessed(list) => {
linesProcessed += 1
result :::= list
//If we are done, we can propagate the result to the creator
if (linesProcessed == totalLines) {
master.map(_ ! result)
}
}
case _ => log.error("message not recognized")
}
}
class Consumer extends Actor with ActorLogging {
def tokenize(line: String): Seq[String] = {
line.split(" ").map(_.toLowerCase)
}
override def receive = {
case Sentence(sent) => {
//Assume: This is representative for the extensive computation method
val tokens = tokenize(sent)
sender() ! SentenceProcessed(tokens.toList)
}
case _ => log.error("message not recognized")
}
}
class WordCount extends Actor {
val consumers = context.actorOf(Props[Consumer].
withRouter(FromConfig()).
withDispatcher("consumer-dispatcher"), "consumers")
val producer = context.actorOf(Props(new Producer(consumers)), "producer")
context.watch(consumers)
context.watch(producer)
def receive = {
case Terminated(`producer`) => consumers ! Broadcast(PoisonPill)
case Terminated(`consumers`) => context.system.shutdown
}
}
object WordCount {
def getActor() = new WordCount
def getConfig(routerType: String, dispatcherType: String)(numConsumers: Int) = s"""
akka.actor.deployment {
/WordCount/consumers {
router = $routerType
nr-of-instances = $numConsumers
dispatcher = consumer-dispatcher
}
}
consumer-dispatcher {
type = $dispatcherType
executor = "fork-join-executor"
}"""
}
WordCount
actor负责创建其他actor。完成Consumer
后,Producer
会发送包含所有令牌的邮件。但是,如何再次传播消息并接受并等待它?第三个WordCount
actor的架构可能是错误的。
case class Run(name: String, actor: () => Actor, config: (Int) => String)
object Main extends App {
val run = Run("push_implementation", WordCount.getActor _, WordCount.getConfig("balancing-pool", "Dispatcher") _)
def execute(run: Run, numConsumers: Int) = {
val config = ConfigFactory.parseString(run.config(numConsumers))
val system = ActorSystem("Counting", ConfigFactory.load(config))
val startTime = System.currentTimeMillis
system.actorOf(Props(run.actor()), "WordCount")
/*
How to get the result here?!
*/
system.awaitTermination
System.currentTimeMillis - startTime
}
execute(run, 4)
}
如您所见,实际问题是将结果传播回Main
例程。你能告诉我如何以正确的方式做到这一点吗?问题是如何在消费者完成之前等待结果?我简要介绍了Akka Future
文档部分,但整个系统对初学者来说有点压倒性。像var future = message ? actor
这样的东西似乎合适。不确定,怎么做。使用WordCount
actor会导致额外的复杂性。也许有可能提出一个不需要这个演员的解决方案?
答案 0 :(得分:2)
考虑使用Akka Aggregator Pattern。这照顾了低级原语(观看演员,毒丸等)。您可以专注于管理州。
您对system.actorOf()
的通话会返回ActorRef
,但您没有使用它。你应该向演员询问结果。像这样:
implicit val timeout = Timeout(5 seconds)
val wCount = system.actorOf(Props(run.actor()), "WordCount")
val answer = Await.result(wCount ? "sent.txt", timeout.duration)
这意味着您的WordCount
类需要receive
方法接受String
消息。该部分代码应汇总结果并告诉sender()
,如下所示:
class WordCount extends Actor {
def receive: Receive = {
case filename: String =>
// do all of your code here, using filename
sender() ! results
}
}
此外,您可以应用一些技巧来处理Futures,而不是使用上面的Await
屏蔽结果。