future

A better syntax for recovery from a for comprehension

匆匆过客 提交于 2019-12-04 11:17:22
I have a number of functions that return a future that is the result of a for comprehension, but i need to need to recover from some possible failures on the way out. The standard syntax seems to capture the for comprehension as an intermediate results like so: def fooBar(): Future[String] = { val x = for { x <- foo() y <- bar(x) } yield y x.recover { case SomeException() => "bah" } } The best alternative to I've found is to wrap the whole for comprehension in parentheses: def fooBar(): Future[String] = (for { x <- foo() y <- bar(x) } yield y).recover { case SomeException() => "bah" } This

Whether method cancel() in java.util.concurrent.Future should be blocking?

懵懂的女人 提交于 2019-12-04 10:52:42
I'm trying to implement Future<> interface in my project. But it looks like documentation is a little bit vague for it. From official documentation we can deduce: Method cancel() does not throw exceptions like InterruptedException or ExecutionException. Moreover it has no the variant with timeout. So it looks like, it should NOT be blocking. Documentation says After this method returns, subsequent calls to isDone() will always return true. but boolean isDone() Returns true if this task completed. So if we run cancel() while the task is processing and cannot be canceled, this method should wait

Sequencing `Future`s with timeout

限于喜欢 提交于 2019-12-04 10:51:45
I utilized the TimeoutScheduler introduced at Scala Futures - built in timeout? . However, now my program does not terminate as before without TimeoutScheduler . I have two Future s: res1 and res2 . Both with a timeout of 15 seconds. In the end I sequence both Future s in order to shutdown the HTTP executor properly in the onComplete callback. Without using withTimeout the program terminates right after http.shutdown . But with using withTimeout is doesn't. Why? There must be some further futures... import java.net.URI import scala.util.{ Try, Failure, Success } import dispatch._ import org

How can I get a future from boost::asio::post?

蹲街弑〆低调 提交于 2019-12-04 09:58:11
I am using Boost 1.66.0, in which asio has built-in support for interoperating with futures (and for some time now). The examples I've seen online indicate how to achieve this cleanly when using networking functions such as async_read , async_read_some , etc. That is done by providing boost::asio::use_future in place of the completion handler, which causes the initiating function to return a future as expected. What kind of object do I need to provide or wrap my function in to get the same behavior from boost::asio::post ? My purpose for posting the work is to execute it in the context of a

Akka Futures Exceptions

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 09:35:02
问题 What happens when an actor of a future throws an exception? According to the Akka documentation at http://doc.akka.io/docs/akka/snapshot/scala/futures.html: It doesn't matter if an Actor or the dispatcher is completing the Future, if an Exception is caught the Future will contain it instead of a valid result. If a Future does contain an Exception, calling Await.result will cause it to be thrown again so it can be handled properly. I am not sure this is what I am seeing when running this piece

Dealing with failed futures

 ̄綄美尐妖づ 提交于 2019-12-04 09:24:30
In Play Framework 2.3, an action can produce a result from a successful future call like this: def index = Action.async { val futureInt = scala.concurrent.Future { intensiveComputation() } futureInt.map(i => Ok("Got result: " + i)) } But how can an action deal with a failed future call, i.e., a future that was completed by calling failure() instead of success() ? For instance, how could an action produce a InternalServerError result with the message returned in the future's failure's throwable? onComplete and onFailure callbacks don't seem to fit the action's flow (it needs to return a result,

Run multiple futures in parallel, return default value on timeout

雨燕双飞 提交于 2019-12-04 08:25:23
问题 I have to run multiple futures in parallel and the program shouldn't crash or hang. For now I wait on futures one by one, and use fallback value if there is TimeoutException. val future1 = // start future1 val future2 = // start future2 val future3 = // start future3 // <- at this point all 3 futures are running // waits for maximum of timeout1 seconds val res1 = toFallback(future1, timeout1, Map[String, Int]()) // .. timeout2 seconds val res2 = toFallback(future2, timeout2, List[Int]()) // .

NHibernate Future Object Graph Many Queries

时间秒杀一切 提交于 2019-12-04 05:45:22
Given a multi level object graph being called using Future as: var Dads = db.Session.Query<Parent>().Where(P => P.EntityKey == Id) .ToFuture<Parent>(); var Kids = db.Session.Query<Kid>().Where(K => K.Parent.EntityKey == Id) .ToFuture<Kid>(); when I call var Dad = dads.ToList() I see the batch go across the wire and show in profiler. Problem is when enumerating the collection it is still sending one off queries to the db Eg. for each (Kid kid in Dad.Kids) // This seems to hit the database { Teach(kid); } Sends a SQL query and hits the database to get each kid. Why is the object graph not

Flutter/Dart - calling a function that is a Future<String> … but needs to return only a String

微笑、不失礼 提交于 2019-12-04 05:22:12
问题 I have an async function that is calling out to Firestore to pull in a data value. I got a lot of help in a previous post...learned a lot...and wanted to start over with hopefully a cleaner question. So I have the following function Future<String> getSetList () async { DocumentReference set01DocRef = Firestore.instance.collection('sets').document('SET01'); var snapshot = await set01DocRef.get(); songList = snapshot['songs']; //works, get expected text value from FS return songList; } This

React for futures

我是研究僧i 提交于 2019-12-04 04:27:09
I am trying to use a divide-and-conquer (aka fork/join) approach for a number crunching problem. Here is the code: import scala.actors.Futures.future private def compute( input: Input ):Result = { if( pairs.size < SIZE_LIMIT ) { computeSequential() } else { val (input1,input2) = input.split val f1 = future( compute(input1) ) val f2 = future( compute(input2) ) val result1 = f1() val result2 = f2() merge(result1,result2) } } It runs (with a nice speed-up) but the the future apply method seems to block a thread and the thread pool increases tremendously. And when too many threads are created, the