多个Scala actor为一项任务提供服务

时间:2011-03-24 11:32:52

标签: scala concurrency actor simd

我需要并行处理多个数据值(“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
}

如何使用Actors做同样的事情?我不相信我想把所有进程“提供”给一个actor,因为actor会依次执行它们。

我是否需要创建多个“处理器”角色,其中“调度员”角色向每个“处理器”角色发送相同数量的进程?

2 个答案:

答案 0 :(得分:10)

如果您只是想要进行“即发即弃”处理,为什么不使用Scala期货呢?

import scala.actors.Futures._
def example = {
  val answers = (1 to 4).map(x => future {
    Thread.sleep(x*1000)
    println("Slept for "+x)
    x
  })
  val t0 = System.nanoTime
  awaitAll(1000000,answers: _*)  // Number is timeout in ms
  val t1 = System.nanoTime
  printf("%.3f seconds elapsed\n",(t1-t0)*1e-9)
  answers.map(_()).sum
}

scala> example
Slept for 1
Slept for 2
Slept for 3
Slept for 4
4.000 seconds elapsed
res1: Int = 10

基本上,你要做的只是将你想要的代码放在future { }块中,它会立即返回未来;应用它来获得答案(它会阻塞直到完成),或者使用awaitAll超时等待每个人都完成。


更新:从2.11开始,执行此操作的方法是使用scala.concurrent.Future。上述代码的翻译是:

import scala.concurrent._
import duration._
import ExecutionContext.Implicits.global

def example = {
  val answers = Future.sequence(
    (1 to 4).map(x => Future {
      Thread.sleep(x*1000)
      println("Slept for "+x)
      x
    })
  )
  val t0 = System.nanoTime
  val completed = Await.result(answers, Duration(1000, SECONDS))
  val t1 = System.nanoTime
  printf("%.3f seconds elapsed\n",(t1-t0)*1e-9)
  completed.sum
}

答案 1 :(得分:3)

如果您可以使用Akka,请查看ActorPool支持:http://doc.akka.io/routing-scala

它允许您指定有关要并行运行的actor的数量的参数,然后将工作分派给这些actor。