future

What is the best way to handle an ExecutionException?

独自空忆成欢 提交于 2019-11-26 19:08:48
问题 I have a method that performs some task with a timeout. I use the ExecutorServer.submit() to get a Future object, and then I call future.get() with a timeout. This is working fine, but my question is the best way to handle checked exceptions that can be thrown by my task. The following code works, and preserves the checked exceptions, but it seems extremely clumsy and prone to break if the list of checked exceptions in the method signature changes. Any suggestions on how to fix this? I need

Future task of ExecutorService not truly cancelling

独自空忆成欢 提交于 2019-11-26 18:19:15
问题 I push my Futures from a ExecutorService into a hash map. Later, I may call cancel on Futures from within the hash map. Although the result is true, I later hit breakpoints within the Callable procedure, as if the Future cancel() had no effect. I think it might be a case of two different references here (even though the reference IDs are listed as the same when breakpointing), but was wondering if some experts could chime in. Here's what the code looks like: ExecutorService taskExecutor =

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

南笙酒味 提交于 2019-11-26 17:57:45
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, the polling phase of the future must be non-blocking to work well. The I/O I want to encapsulate can

Get the status of a std::future

我怕爱的太早我们不能终老 提交于 2019-11-26 17:31:18
问题 Is it possible to check if a std::future has finished or not? As far as I can tell the only way to do it would be to call wait_for with a zero duration and check if the status is ready or not, but is there a better way? 回答1: You are correct, and apart from calling wait_until with a time in the past (which is equivalent) there is no better way. You could always write a little wrapper if you want a more convenient syntax: template<typename R> bool is_ready(std::future<R> const& f) { return f

What is the purpose of async/await in Rust?

安稳与你 提交于 2019-11-26 17:28:25
问题 In a language like C#, giving this code (I am not using the await keyword on purpose): async Task Foo() { var task = LongRunningOperationAsync(); // Some other non-related operation AnotherOperation(); result = task.Result; } In the first line, the long operation is run in another thread, and a Task is returned (that is a future). You can then do another operation that will run in parallel of the first one, and at the end, you can wait for the operation to be finished. I think that it is also

How to cancel Future in Scala?

此生再无相见时 提交于 2019-11-26 17:23:43
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? 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 you want to cancel and a future that comes from a custom Promise . You could then cancel the thus created

Futures do not run before program termination

☆樱花仙子☆ 提交于 2019-11-26 17:02:57
问题 I was trying to reproduce the example on new Scala 2.10 futures feature. The code I've used is: import scala.concurrent.Future import scala.concurrent.future object Test { def main(args: Array[String]) { println("Test print before future") val s = "Hello" val f = future {s + " future!"} f onSuccess {case v => println(v)} println("Test print after future") } } Instead of printing: Test print before future Hello future! Test print after future It simply prints: Test print before future Test

How do I conditionally return different types of futures?

◇◆丶佛笑我妖孽 提交于 2019-11-26 16:43:36
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 | | } else { 9 | | future::ok(10).and_then(|x| future::ok(x + 2)) 10 | | } | |_____^ expected struct

Accessing value returned by scala futures

╄→尐↘猪︶ㄣ 提交于 2019-11-26 15:45:19
问题 I am a newbie to scala futures and I have a doubt regarding the return value of scala futures. So, generally syntax for a scala future is def downloadPage(url: URL) = Future[List[Int]] { } I want to know how to access the List[Int] from some other method which calls this method. In other words, val result = downloadPage("localhost") then what should be the approach to get List[Int] out of the future ? I have tried using map method but not able to do this successfully.` 回答1: The case of

Waiting on a list of Future

梦想与她 提交于 2019-11-26 15:42:45
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 , means somone could not do its task return; } } } But the problem here is if, for example, the 4th