future

Java - Future.get() multiple invocations

两盒软妹~` 提交于 2021-02-06 09:43:04
问题 How does Java's Future.get() behave in the case where it is called multiple times after the task is completed? Does it return the the same result? Or does throw an ExecutionException again and again with the same exception if the computation failed? I can not find anything in the docs about it! 回答1: You can call get() on a Future as often as you like, and it will only block if the task that produces the result has not finished yet. If the task has already finished, it will just immediately

Return CompletableFuture<Void> or CompletableFuture<?>?

拜拜、爱过 提交于 2021-02-05 12:54:51
问题 I want to write an asynchronous method that returns a CompletableFuture. The only purpose of the future is to track when the method is complete, not its result. Would it be better to return CompletableFuture<Void> or CompletableFuture<?> ? Is there a reason to prefer one or the other, or are they interchangeable? CompletableFuture itself returns CompletableFuture<Void> from many of its methods. java.nio has a Future<Void> in AsynchronousSocketChannel: Future<Void> connect(SocketAddress remote

Wait for List of ListenAbleFuture returned by Kafka Send API

感情迁移 提交于 2021-01-29 21:56:01
问题 I have the List of ListenAbleFuture.I want to wait of this List of ListenableFuture<SendResult<Integer, String>> for atmost 15 minutes if they have not comepleted. How can i achieve it. Currently i am doing this but this wait for 15 min for every ListenAbleFuture which is what i dont want. for (ListenableFuture<SendResult<Integer, String>> m : myFutureList) { m.get(15, TimeUnit.MINUTES) ; } ListenableFuture<SendResult<Integer, String>> is from import org.springframework.util.concurrent

How to change working directory in asynchronous futures in R

帅比萌擦擦* 提交于 2021-01-28 06:22:36
问题 I am trying to change working directory in a future processor, carry out some operations, and exit. The problem is I am not able to set a working directory. The following toy example works fine library(future) dirNames <- as.character(c(1:4)) sapply(dirNames, function(x) if(!dir.exists(x)) dir.create(x)) plan(multiprocess, workers=2) b <- list() for(i in seq_along(dirNames)){ sleeptime <- 10 if(i > 3) sleeptime <- 50 a <- future({ # setwd(dirNames[i]) Sys.sleep(sleeptime) return(2) }) print(i

Call method at date/time

倖福魔咒の 提交于 2021-01-28 00:51:23
问题 I am searching for a modern way to execute a given method at a given date/time ( ZonedDateTime in particular). I am aware of the Timer class and the Quartz library, as shown here (the threads include full solutions): Java - Execute method on specific date [closed] Call a method at fixed time in Java But those threads are rather old and do not utilize the new Java features and library elements since then. In particular, it would be very handy to get hands on any kind of Future object, since

python futures and tuple unpacking

故事扮演 提交于 2021-01-27 18:45:50
问题 What is an elagant/idiomatic way to achieve something like tuple unpacking with futures? I have code like a, b, c = f(x) y = g(a, b) z = h(y, c) and I would like to convert it to use futures. Ideally I would like to write something like a, b, c = ex.submit(f, x) y = ex.submit(g, a, b) z = ex.submit(h, y, c) The first line of that throws TypeError: 'Future' object is not iterable though. How can I get a,b,c without having to make 3 additional ex.submit calls? ie. I would like to avoid having

Writing a chunk stream to a file asynchronously using hyper

拥有回忆 提交于 2021-01-27 13:01:48
问题 I am trying to create a simple function that downloads a remote file to a local filepath using hyper. I need the file write to be asynchronous as well (in my case I am using tokio_fs for that). Here is the code: View in the playground // Parts of the code were omitted, see the playground for full source code pub fn download_file( uri: Uri, file_location: &Path, ) -> Box<Future<Item = (), Error = DownloadFileError>> { let temp_dir_path = tempfile::tempdir().unwrap().into_path(); let file_name

Issue passing mutable Arc reference to hyper service_fn handler

蹲街弑〆低调 提交于 2021-01-24 07:09:21
问题 I've been trying the following Relevant imports and code shown use std::sync::{Arc, Mutex}; use std::thread; use hyper::rt::{self, Future, Stream}; use hyper::service::service_fn; use hyper::{Body, Request, Response, Server, StatusCode}; pub struct ChallengeState; pub struct ChallengeResponse; type BoxFut<'a> = Box<Future<Item = Response<Body>, Error = hyper::Error> + Send + 'a>; fn handle_challengeproof<'a>( req: Request<Body>, challenge: &Arc<Mutex<ChallengeState>>, ) -> BoxFut<'a> { let

Timeout while waiting for a batch of Futures to complete?

纵然是瞬间 提交于 2020-12-30 06:50:27
问题 I have a set of Futures created by submitting Callable s to an Executor . Pseudo code: for all tasks futures.add(executor.submit(new callable(task))) Now I'd like to get all futures waiting at most n seconds until all complete. I know I can call Future#get(timeout) but if I call that sequentially for all my futures in a loop the timouts start adding up. Pseudo code: for all futures future.get(timeout) get blocks with a timeout until the result is ready. Therefore, if the first completes just

Timeout while waiting for a batch of Futures to complete?

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-30 06:48:40
问题 I have a set of Futures created by submitting Callable s to an Executor . Pseudo code: for all tasks futures.add(executor.submit(new callable(task))) Now I'd like to get all futures waiting at most n seconds until all complete. I know I can call Future#get(timeout) but if I call that sequentially for all my futures in a loop the timouts start adding up. Pseudo code: for all futures future.get(timeout) get blocks with a timeout until the result is ready. Therefore, if the first completes just