Michi
unread,Jul 30, 2012, 11:16:26 AM7/30/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to scala...@googlegroups.com
Hi,
is it guaranteed that Future.sequence always preserves the order? In the following example I create ten actors which have an ID from 0 to 9. I want to create a list of actors and there IDs:
import akka.actor.{ActorRef, Props, Actor, ActorSystem}
import akka.dispatch.{Future, Await}
import akka.pattern.ask
import akka.util.Timeout
import akka.util.duration._
case object RequestIdMessage
case class IdMessage(id: String)
object Main {
def main(args: Array[String]) {
val system = ActorSystem("Main")
// create a list of actors
val actors = (0 until 10) map (i => system.actorOf(Props(new MyActor(i.toString))))
implicit val timeout = Timeout(5 seconds)
// create a list of futures
val listOfFutures = actors map (((actor: ActorRef) => (actor ? RequestIdMessage).mapTo[IdMessage]))
implicit val ec = system.dispatcher
val futureList = Future.sequence(listOfFutures)
val result = Await.result(futureList, timeout.duration)
println(actors.zip(result))
}
}
class MyActor(id: String) extends Actor {
def receive = {
case RequestIdMessage => sender ! IdMessage(id)
}
}
Apparently Future.sequence does preserve the order, but the API documentation does not state this anywhere. Is it guaranteed that Future.sequence will always preserve the order in the future? If not, how would be the best way to write the program above?
Thanks,
Michael