future

How to wait for several Futures

不想你离开。 提交于 2019-11-26 15:13:59
问题 Suppose I have several futures and need to wait until either any of them fails or all of them succeed. For example: Let there are 3 futures: f1 , f2 , f3 . If f1 succeeds and f2 fails I do not wait for f3 (and return failure to the client). If f2 fails while f1 and f3 are still running I do not wait for them (and return failure ) If f1 succeeds and then f2 succeeds I continue waiting for f3 . How would you implement it? 回答1: You could use a for-comprehension as follows instead: val fut1 =

How do I make a function involving futures tail recursive?

两盒软妹~` 提交于 2019-11-26 12:46:56
问题 In my Scala app, I have a function that calls a function which returns a result of type Future[T]. I need to pass the mapped result in my recursive function call. I want this to be tail recursive, but the map (or flatMap) is breaking the ability to do that. I get an error \"Recursive call not in tail position.\" Below is a simple example of this scenario. How can this be modified so that the call will be tail recursive (without subverting the benefits of Futures with an Await.result())?

Why does Future::select choose the future with a longer sleep period first?

南楼画角 提交于 2019-11-26 10:03:33
问题 I\'m trying to understand Future::select: in this example, the future with a longer time delay is returned first. When I read this article with its example, I get cognitive dissonance. The author writes: The select function runs two (or more in case of select_all ) futures and returns the first one coming to completion. This is useful for implementing timeouts. It seems I don\'t understand the sense of select . extern crate futures; extern crate tokio_core; use std::thread; use std::time:

Transform Java Future into a CompletableFuture

笑着哭i 提交于 2019-11-26 08:13:50
问题 Java 8 introduces CompletableFuture , a new implementation of Future that is composable (includes a bunch of thenXxx methods). I\'d like to use this exclusively, but many of the libraries I want to use return only non-composable Future instances. Is there a way to wrap up a returned Future instances inside of a CompleteableFuture so that I can compose it? 回答1: There is a way, but you won't like it. The following method transforms a Future<T> into a CompletableFuture<T> : public static <T>

How do I synchronously return a value calculated in an asynchronous Future in stable Rust?

ε祈祈猫儿з 提交于 2019-11-26 07:49:14
问题 I am trying to use hyper to grab the content of an HTML page and would like to synchronously return the output of a future. I realized I could have picked a better example since synchronous HTTP requests already exist, but I am more interested in understanding whether we could return a value from an async calculation. extern crate futures; extern crate hyper; extern crate hyper_tls; extern crate tokio; use futures::{future, Future, Stream}; use hyper::Client; use hyper::Uri; use hyper_tls:

What is the best approach to encapsulate blocking I/O in future-rs?

余生长醉 提交于 2019-11-26 06:03:29
问题 I read the tokio documentation and I wonder what is the best approach for encapsulating costly synchronous I/O in a future. With the reactor framework, we get the advantage of a green threading model: a few OS threads handle a lot of concurrent tasks through an executor. The future model of tokio is demand driven, which means the future itself will poll its internal state to provide informations about its completion; allowing backpressure and cancellation capabilities. As far as I understand,

How do I conditionally return different types of futures?

拟墨画扇 提交于 2019-11-26 05:56:40
问题 I have a method that, depending on a predicate, will return one future or another. In other words, an if-else expression that returns a future: extern crate futures; // 0.1.23 use futures::{future, Future}; fn f() -> impl Future<Item = usize, Error = ()> { if 1 > 0 { future::ok(2).map(|x| x) } else { future::ok(10).and_then(|x| future::ok(x + 2)) } } This doesn\'t compile: error[E0308]: if and else have incompatible types --> src/lib.rs:6:5 | 6 | / if 1 > 0 { 7 | | future::ok(2).map(|x| x) 8

How to cancel Future in Scala?

ぐ巨炮叔叔 提交于 2019-11-26 05:25:33
问题 Java Future has cancel method, which can interrupt the thread, which runs the Future task. For example, if I wrap an interruptible blocking call in a Java Future I can interrupt it later. Scala Future provides no cancel method. Suppose I wrap an interruptible blocking call in a Scala Future . How can I interrupt it? 回答1: This is not yet a part of the Future s API, but may be added as an extension in the future. As a workaround, you could use the firstCompletedOf to wrap 2 futures - the future

Waiting on a list of Future

末鹿安然 提交于 2019-11-26 04:35:03
问题 I have a method which returns a List of futures List<Future<O>> futures = getFutures(); Now I want to wait until either all futures are done processing successfully or any of the tasks whose output is returned by a future throws an exception. Even if one task throws an exception, there is no point in waiting for the other futures. Simple approach would be to wait() { For(Future f : futures) { try { f.get(); } catch(Exception e) { //TODO catch specific exception // this future threw exception

How to Eager Load Associations without duplication in NHibernate?

感情迁移 提交于 2019-11-26 00:08:33
问题 I\'d need to load a list of very large objects with so many children and children of children. what\'s the best approach to take? I\'m using Oracle 11g database and I\'ve written the below method but it results in cartesian product (duplicated results): public IList<ARNomination> GetByEventId(long eventId) { var session = this._sessionFactory.Session; var nominationQuery = session.Query<ARNomination>().Where(n => n.Event.Id == eventId); using (var trans = session.Transaction) { trans.Begin();