future

How to get out value from Scala Future onComplete/onSuccess

馋奶兔 提交于 2019-12-13 08:45:29
问题 I have a high level structure of my code as follows. This is just an example replicating the high level structure.:- import scala.concurrent.Future class FutureReturnsAValue extends PersonAgeModifier { def main(args: Array[String]) { val jhonObj = Person("Jhon", 25) val punishmentResult = addAgeCurse(jhonObj) println("The punishment result for Jhonny is " + punishmentResult) } def addAgeCurse(person: Person): String = { val oldAge = person.age val futureAge = LongProcessingOpForAge(person)

Scala - Futures and Eithers

元气小坏坏 提交于 2019-12-13 06:38:51
问题 this thread gave me an idea how to structure my code: Scala-way to handle conditions in for-comprehensions? The part in question: // First create the JSON val resultFuture: Future[Either[Failure, JsResult]] = for { userRes <- userDao.findUser(userId) user <- userRes.withFailure(UserNotFound).right authRes <- userDao.authenticate(user) auth <- authRes.withFailure(NotAuthenticated).right goodRes <- goodDao.findGood(goodId) good <- goodRes.withFailure(GoodNotFound).right checkedGood <- checkGood

How to retrieve partial result in multiple Future wait when some might time-out?

家住魔仙堡 提交于 2019-12-13 04:37:34
问题 I am using FUTURE in scala on play framework. But I have difficulty to get part of the final result in case of timeout when merging multiple futures. Here is what my code does. It has two future to query two providers respectively. And then use a for/yield statement to merge the results. And then await for the result with a time out value. It works fine when two providers reply to the query on time. However, if just one provider reply on time, I know the await will timeout but in this case I

Closure in onFailure continuation for a scala.concurrent.Future not working as expected

送分小仙女□ 提交于 2019-12-13 03:22:26
问题 I'm encountering an issue where I have two methods, the first calling the second in a loop and the second creating a Future as follows: public class WorkersCoordinator { private static Logger LOGGER = LoggerFactory.getLogger(WorkersCoordinator.class); private final Timeout timeout; private final ActorSystem system; private final List<Class<? extends BaseWorker>> workers; private final Map<Class, ActorRef> refMap; private final WorkResultPackageQueue pkgQueue; private final ActorFactory

Flutter: Correct approach to get value from Future

僤鯓⒐⒋嵵緔 提交于 2019-12-13 03:19:12
问题 I have a function which returns images directory path, it performs some additional check like if directory exists or not, then it behaves accordingly. Here is my code: Future<String> getImagesPath() async { final Directory appDir = await getApplicationDocumentsDirectory(); final String appDirPath = appDir.path; final String imgPath = appDirPath + '/data/images'; final imgDir = new Directory(imgPath); bool dirExists = await imgDir.exists(); if (!dirExists) { await new Directory(imgPath).create

parallel quick sort outdone by single threaded quicksort

落爺英雄遲暮 提交于 2019-12-13 02:29:08
问题 I've been reading the book C++ concurrency in action, here is the example in the book using futures to implement parallel quick sort. But I found this function is more than twice slower than the single threaded quick sort function without using any asynchronous facilities in c++ standard library. Tested with g++ 4.8 and visual c++ 2012. I used 10M random integers to test, and in visual c++ 2012,this function spawned 6 threads in total to perform the operation in my quad core PC. I am really

Loading complex graph in nhibernate

蓝咒 提交于 2019-12-13 01:55:17
问题 Imagina i have pretty complex model graph such as for an example: Orchestra -> Musicians -> Instruments -> Properties -> Items -> Songs -> Parts I know in theory how Futures works but how do i load this complete graph lets say for a concrete musician (specified by id). I know that for each collection on same level i have to create simple one future query to avoid Cartesian product in same query. so when i execute code like this: using(var session = GetSession()){ var instrumentQuery = session

How to add callbacks to Future in Scala?

跟風遠走 提交于 2019-12-12 18:26:01
问题 I saw an example here: val fut = Future { ... // my body function } // my body function starts here fut onComplete { ... // my callback } Looks like I may add the callback after the completion of my body function. Is it still invoked ? Anyway, I would prefer to add callbacks to a future before my function starts running. Does it make sense ? How can I do that ? 回答1: If you want to control the point of execution of a future, you could chain it with a Promise . import scala.concurrent._ import

Concatenate many Future[Seq] into one Future[Seq]

拥有回忆 提交于 2019-12-12 15:19:15
问题 Without Future, that's how I combine all smaller Seq into one big Seq with a flatmap category.getCategoryUrlKey(id: Int):Seq[Meta] // main method val appDomains: Seq[Int] val categories:Seq[Meta] = appDomains.flatMap(category.getCategoryUrlKey(_)) Now the method getCategoryUrlKey could fail. I put a circuit breaker in front to avoid to call it for the next elements after an amount of maxFailures . Now the circuit breaker doesn't return a Seq but a Future[Seq] lazy val breaker = new akka

Calculate the list of the futures and and return the result future

余生颓废 提交于 2019-12-12 15:08:15
问题 I have a function which takes futures Future[A]* and I want it to return a Future[List[A]] . def singleFuture[T](futures: List[Future[A]]): Future[List[A]] = { val p = Promise[T] futures filter { _ onComplete { case x => p complete x /*????*/ } } p.future } And I also want the result future of type Future[List[A]] becomes completed immediately after the list futures List[Future[A]] have been completed. That code doesn't work. I figure I should use flatMap here because there should be 2