多个 Scala actor 服务于一项任务
我需要并行处理多个数据值(“SIMD”)。我可以使用 java.util.concurrent
API (Executors.newFixedThreadPool()
) 使用 Future
实例并行处理多个值:
import java.util.concurrent.{Executors, Callable}
class ExecutorsTest {
private class Process(value: Int)
extends Callable[Int] {
def call(): Int = {
// Do some time-consuming task
value
}
}
val executorService = {
val threads = Runtime.getRuntime.availableProcessors
Executors.newFixedThreadPool(threads)
}
val processes = for (process <- 1 to 1000) yield new Process(process)
val futures = executorService.invokeAll(processes)
// Wait for futures
}
如何我使用演员做同样的事情?我不认为我想将所有进程“馈送到”单个参与者,因为参与者随后将按顺序执行它们。
我是否需要创建多个“处理器”参与者,并使用“调度程序”参与者向每个“处理器”参与者发送相同数量的进程?
I need to process multiple data values in parallel ("SIMD"). I can use the java.util.concurrent
APIs (Executors.newFixedThreadPool()
) to process several values in parallels using Future
instances:
import java.util.concurrent.{Executors, Callable}
class ExecutorsTest {
private class Process(value: Int)
extends Callable[Int] {
def call(): Int = {
// Do some time-consuming task
value
}
}
val executorService = {
val threads = Runtime.getRuntime.availableProcessors
Executors.newFixedThreadPool(threads)
}
val processes = for (process <- 1 to 1000) yield new Process(process)
val futures = executorService.invokeAll(processes)
// Wait for futures
}
How do I do the same thing using Actors? I do not believe that I want to "feed" all of the processes to a single actor because the actor will then execute them sequentially.
Do I need to create multiple "processor" actors with a "dispatcher" actor that sends an equal number of processes to each "processor" actor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想“即发即忘”处理,为什么不使用 Scala future?
基本上,您所做的就是将所需的代码放入
future { }
块中,它将立即返回一个 future;应用它来获取答案(它将阻塞直到完成),或者使用带有超时的awaitAll
来等待每个人都完成。更新:从 2.11 开始,可以使用 scala.concurrent.Future 来执行此操作。上述代码的翻译是:
If you just want fire-and-forget processing, why not use Scala futures?
Basically, all you do is put the code you want inside a
future { }
block, and it will immediately return a future; apply it to get the answer (it will block until done), or useawaitAll
with a timeout to wait until everyone is done.Update: As of 2.11, the way to do this is with
scala.concurrent.Future
. A translation of the above code is:如果您可以使用 Akka,请查看 ActorPool 支持: http://doc.akka.io/routing -scala
它允许您指定有关要并行运行的参与者数量的参数,然后将工作分派给这些参与者。
If you can use Akka, take a look at the ActorPool support: http://doc.akka.io/routing-scala
It lets you specify parameters about how many actors you want running in parallel and then dispatches work to those actors.