28

Вопрос по concurrency – Могут ли актеры Scala обрабатывать несколько сообщений одновременно?

Ответ [https://stackoverflow.com/questions/1005659/scala-actors-as-single-threaded-queues/1006491#1006491] Error: User Rate Limit Exceededone at a timeError: User Rate Limit Exceeded Programming in ScalaError: User Rate Limit Exceeded > If [the ...

Error: User Rate Limit Exceeded

от Ken Bloom

Error: User Rate Limit Exceeded

от oxbow_lakes

3 ответа

0

Error: User Rate Limit Exceeded

6

Error: User Rate Limit ExceededActorError: User Rate Limit ExceededActorError: User Rate Limit Exceededcan be handled asynchronouslyError: User Rate Limit Exceeded

val actor_ = actor {

  loop {
    react {
      case msg =>
        //create a new actor to execute the work. The framework can then 
        //manage the resources effectively
        actor {
          //do work here
        }
      }
    }
  }
26

Error: User Rate Limit Exceeded

import scala.actors.Actor
import scala.actors.Actor._

case class Request(sender : Actor, payload : String)
case class Ready(sender : Actor)
case class Result(result : String)
case object Stop

def consumer(n : Int) = actor {
  loop {
    react {
      case Ready(sender) => 
        sender ! Ready(self)
      case Request(sender, payload) =>
        println("request to consumer " + n + " with " + payload)
        // some silly computation so the process takes awhile
        val result = ((payload + payload + payload) map {case '0' => 'X'; case '1' => "-"; case c => c}).mkString
        sender ! Result(result)
        println("consumer " + n + " is done processing " + result )
      case Stop => exit
    }
  }
}

// a pool of 10 consumers
val consumers = for (n <- 0 to 10) yield consumer(n)

val coordinator = actor {
  loop {
     react {
        case msg @ Request(sender, payload) =>
           consumers foreach {_ ! Ready(self)}
           react {
              // send the request to the first available consumer
              case Ready(consumer) => consumer ! msg
           }
         case Stop => 
           consumers foreach {_ ! Stop} 
           exit
     }
  }
}

// a little test loop - note that it's not doing anything with the results or telling the coordinator to stop
for (i <- 0 to 1000) coordinator ! Request(self, i.toString)

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

import scala.actors.Futures._

def transform(payload : String) = {      
  val result = ((payload + payload + payload) map {case '0' => 'X'; case '1' => "-"; case c => c}).mkString
  println("transformed " + payload + " to " + result )
  result
}

val results = for (i <- 0 to 1000) yield future(transform(i.toString))

Похожие вопросы