Hi, In general I would consider Agents to be an advanced feature. It might be tempting to use them since they look "simple" but I recommend using Actors instead whenever it is possible. While there are important use-cases of Agents they are for fairly specific scenarios.
-Endre On Thu, Feb 27, 2014 at 10:41 AM, √iktor Ҡlang <[email protected]>wrote: > Also, note that the Agent cannot receive any new message until the Future > is completed, which essentially means head stalling. > > > On Thu, Feb 27, 2014 at 10:33 AM, Roland Kuhn <[email protected]> wrote: > >> Why do you want to use an Agent in this case? The actor itself can very >> well store the cached value, which would be more efficient and also >> semantically straight-forward: you can implement any caching strategy you >> like. >> >> One word about your proposal: if I understand you correctly, you need the >> cache to see writes in-order, which you achieve by completely serializing >> them, turning it into a synchronous backend. I am pretty sure that that is >> a bad idea. But then I might have misunderstood what you actually want to >> achieve, which might mean that you need to think a bit more on your desired >> cache semantics and formulate them clearly—not in pseudo-code. >> >> Regards, >> >> Roland >> >> 27 feb 2014 kl. 10:01 skrev [email protected]: >> >> Adding more detail, this is just stub code, may actually not compile, but >> just for illustration: >> >> class A extends Actor { >> val cache = Agent(42) >> val backend: ActorRef // reference to the backend >> >> def receive: Receive = { >> case Read => cache.future() pipeTo sender >> >> case req: Write => { >> // ordering problem here >> backend ? req map { res => cache.send(req.value) } >> >> // here I would like to have something like >> // cache.sendOff(backend ? req map { res => req.value }) >> } >> } >> } >> >> On Wednesday, February 26, 2014 10:59:10 PM UTC-8, rkuhn wrote: >>> >>> >>> 27 feb 2014 kl. 02:34 skrev [email protected]: >>> >>> Hello, >>> >>> If you do the following: >>> future1 map { r => agent.send(1) } >>> future2 map { r => agent.send(2) } >>> >>> The order of the sends will be determined by the length of future1 and >>> future2. >>> >>> >>> Yes, but why should that matter given that you don’t care when >>> future{1,2} are actually executed? >>> >>> I would expect something like >>> agent.sendOff(future1 map { r => 1 }) >>> agent.sendOff(future2 map { r => 2 }) >>> >>> >>> The Future API also supports sequencing operations, which seem more >>> appropriate to use in this case: >>> >>> val future3 = future1.andThen(agent.send(1)).flatMap(_ => >>> future2).andThen(agent.send(2)) >>> >>> future3 completes with the value of future2, but only after the second >>> agent.send() has been executed. >>> >>> Regards, >>> >>> Roland >>> >>> >>> In a way the sends are applied in order. >>> >>> Regards, >>> partycoder >>> >>> On Thursday, February 13, 2014 3:43:14 AM UTC-8, rkuhn wrote: >>>> >>>> Hi partycoder, >>>> >>>> in which way does your code cause an ordering problem? What do you >>>> observe and why is that not as it should be? >>>> >>>> Regards, >>>> >>>> Roland >>>> >>>> 12 feb 2014 kl. 06:36 skrev [email protected]: >>>> >>>> Hello, >>>> >>>> I have a question. >>>> >>>> val f = some future >>>> val a = Agent(some type) >>>> >>>> // This may cause an ordering problem >>>> f map { result => a.send(result) } >>>> >>>> How do I use agent.send/sendOff using futures? >>>> >>>> Regards >>>> >>>> -- >>>> >>>>>>>>>> Read the docs: http://akka.io/docs/ >>>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/ >>>> >>>>>>>>>> Search the archives: https://groups.google.com/ >>>> group/akka-user >>>> --- >>>> You received this message because you are subscribed to the Google >>>> Groups "Akka User List" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to [email protected]. >>>> To post to this group, send email to [email protected]. >>>> Visit this group at http://groups.google.com/group/akka-user. >>>> For more options, visit https://groups.google.com/groups/opt_out. >>>> >>>> >>>> >>>> >>>> *Dr. Roland Kuhn* >>>> *Akka Tech Lead* >>>> Typesafe <http://typesafe.com/> – Reactive apps on the JVM. >>>> twitter: @rolandkuhn >>>> <http://twitter.com/#!/rolandkuhn> >>>> >>>> >>> -- >>> >>>>>>>>>> Read the docs: http://akka.io/docs/ >>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/ >>> >>>>>>>>>> Search the archives: https://groups.google.com/ >>> group/akka-user >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "Akka User List" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at http://groups.google.com/group/akka-user. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >>> >>> >>> >>> *Dr. Roland Kuhn* >>> *Akka Tech Lead* >>> Typesafe <http://typesafe.com/> – Reactive apps on the JVM. >>> twitter: @rolandkuhn >>> <http://twitter.com/#!/rolandkuhn> >>> >>> >> -- >> >>>>>>>>>> Read the docs: http://akka.io/docs/ >> >>>>>>>>>> Check the FAQ: http://akka.io/faq/ >> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user >> --- >> You received this message because you are subscribed to the Google Groups >> "Akka User List" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/akka-user. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> >> >> *Dr. Roland Kuhn* >> *Akka Tech Lead* >> Typesafe <http://typesafe.com/> – Reactive apps on the JVM. >> twitter: @rolandkuhn >> <http://twitter.com/#!/rolandkuhn> >> >> -- >> >>>>>>>>>> Read the docs: http://akka.io/docs/ >> >>>>>>>>>> Check the FAQ: http://akka.io/faq/ >> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user >> --- >> You received this message because you are subscribed to the Google Groups >> "Akka User List" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/akka-user. >> For more options, visit https://groups.google.com/groups/opt_out. >> > > > > -- > Cheers, > √ > > * ——————— **Viktor Klang* > *Chief Architect - **Typesafe <http://www.typesafe.com/>* > > Twitter: @viktorklang > > -- > >>>>>>>>>> Read the docs: http://akka.io/docs/ > >>>>>>>>>> Check the FAQ: http://akka.io/faq/ > >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user > --- > You received this message because you are subscribed to the Google Groups > "Akka User List" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/groups/opt_out. > -- Akka Team Typesafe - The software stack for applications that scale Blog: letitcrash.com Twitter: @akkateam -- >>>>>>>>>> Read the docs: http://akka.io/docs/ >>>>>>>>>> Check the FAQ: http://akka.io/faq/ >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user --- You received this message because you are subscribed to the Google Groups "Akka User List" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/groups/opt_out.
