[英]Scala asynchronous calculation using akka
im trying to design a program that will get a set of inputs (lets say some numbers) 即时通讯试图设计一个程序,将获得一组输入(让说一些数字)
it would do some calculation on each of the input and gather the result 它将对每个输入进行一些计算并收集结果
I need this to be asynchronous calculation. 我需要这是异步计算。
Looked into scala and akka and it looks perfect but i cant seem to understand the design of my actors and who will do that ? 研究了scala和akka,它看起来很完美,但是我似乎无法理解演员的设计,谁来做呢?
for example : create 1 actor for each calc ? 例如:为每个计算创建1个actor? and 1 actor to collect all the results ? 和1位演员收集所有结果?
any idea or documentations for designs using akka ? 关于使用akka进行设计的任何想法或文档?
Thanks! 谢谢!
Look at this sample project 看这个样本项目
this is a example for a akka actor system that has 1 actor that gets a input set 这是一个akka actor系统的示例,该系统具有1个可获取输入集的actor
for each input is will create a child actor that will calculate this and in the end will show the results from all the child actors 对于每个输入,将创建一个子演员,该子演员将对此进行计算,最后将显示所有子演员的结果
readme : https://github.com/Nimrod007/Akka-Compute 自述文件: https : //github.com/Nimrod007/Akka-Compute
code : https://github.com/Nimrod007/Akka-Compute/blob/master/akkaComputeExample/src/main/scala/AkkaComputeApp.scala 代码: https : //github.com/Nimrod007/Akka-Compute/blob/master/akkaComputeExample/src/main/scala/AkkaComputeApp.scala
I'm not answering your question directly since Nimrod007 already did it. 因为Nimrod007已经回答了我的问题,所以我不会直接回答你的问题。 I'm not sure what your actual application is, but I just wanted to show you that using straight Futures might be a lot simpler. 我不确定您的实际应用是什么,但是我只是想向您展示使用直接期货可能会容易得多。
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
object Example extends App {
val futures = Range(1, 5).map(i => Future { i + 10 })
val result = Future.fold(futures)(List.empty[Int])((answers, value) => value :: answers)
result.onComplete(answersTry => answersTry.foreach(println))
Thread.sleep(1000)
}
It does the same as Nimrod007's code, but is an awful lot shorter. 它的功能与Nimrod007的代码相同,但是要短得多。
I don't mean that you should not use Akka. 我并不是说您不应该使用Akka。 But you should consider what is the most appropriate for your application. 但是,您应该考虑最适合您的应用程序的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.