future

Convert a Java Future to a Scala Future

一曲冷凌霜 提交于 2019-11-29 13:31:35
问题 I have a Java Future object which I would like to convert into a Scala Future . Looking at the j.u.c.Future API, there is nothing much that I could use other than the isDone method. Is this isDone method blocking? Currently this is what I have in my mind: val p = Promise() if (javaFuture.isDone()) p.success(javaFuture.get) Is there a better way to do this? 回答1: How about just wrapping it (I'm assuming there's an implicit ExecutionContext here): val scalaFuture = Future { javaFuture.get } EDIT

How to send data through a futures Stream by writing through the io::Write trait?

蹲街弑〆低调 提交于 2019-11-29 12:29:58
I have a function that takes a &mut io::Write and I'd like to use it to send a streaming response from the actix-web server without having to buffer the whole response. The function is "pushing" the data, and I can't change the function (that's the whole premise of this question) to use async streams or other kind of polling. Currently I'm forced to use &mut Vec (which implements io::Write ) to buffer the whole result and then send the Vec as the response body. However, the response may be large, so I'd rather stream it without buffering. Is there some kind of adapter that would implement io:

Shiny promises future is not working on eventReactive

依然范特西╮ 提交于 2019-11-29 12:18:42
I have an inputButton than when you click it, 2 querys to mysql database are done. One is a heavy one (more than 10 secs) and the other is light (less than 0.01sec to get data). As I want to show the result of this querys on shiny app, I have intendeed to use Promises and Future packages for asyncronous loading. In the example that I show you of my code, I have simulated the SQL querys with the function heavyFunction , which is intended to simulate the heavy query and the ligth one time loads. The issue is that this code is not working for me, because the results of the light query are not

Are Futures in Scala really functional?

£可爱£侵袭症+ 提交于 2019-11-29 07:35:11
I am reading this blog post that claims Futures are not "functional" since they are just wrappers of side-effectful computations. For instance, they contain RPC calls, HTTP requests, etc. Is it correct ? The blog post gives the following example: def twoUsersFeed(a: UserHandle, b: UserHandle) (implicit ec: ExecutionContext): Future[Html] = for { feedA <- usersFeed(a) feedB <- usersFeed(b) } yield feedA ++ feedB you lose the desired property: consistent results (the referential transparency). Also you lose the property of making as few requests as possible. It is difficult to use multi-valued

Must you join on a Thread to ensure its computation is complete

删除回忆录丶 提交于 2019-11-29 07:34:46
I have a utility method (used for unit testing, it so happens) that executes a Runnable in another thread. It starts the thread running, but does not wait for the Thread to finish, instead relying on a Future . A caller of the method is expected to get() that Future . But is that enough to ensure safe publication of the computation done by the Runnable ? Here is the method: private static Future<Void> runInOtherThread(final CountDownLatch ready, final Runnable operation) { final CompletableFuture<Void> future = new CompletableFuture<Void>(); final Thread thread = new Thread(() -> { try { ready

Choosing optimal number of Threads for parallel processing of data

淺唱寂寞╮ 提交于 2019-11-29 07:16:22
Let's say I have a task with processing 1 million sentences. For each sentence, I need to do something with it, and it makes no matter what particular order they are processed in. In my Java program I have a set of futures partitioned from my main chunk of work with a callable that defines the unit of work to be done on a chunk of sentences, and I'm looking for a way to optimize the number of threads I allocate to work through the big block of sentences, and later recombine all the results of each thread. What would be the maximum number of threads I could use that would give me optimal

failure in Scala future's for comprehension

元气小坏坏 提交于 2019-11-29 04:16:38
问题 I have three sequential Futures and use in the for comprehension like this val comF = for { f1 <- future1 f2 <- future2 f3 <- future3 } yield { // something } comF onSuccess { } comF onFailure { // ---------------- Here is the problem -------------------------------- // // How do I know which future failed(throw exception), when the callback comes here ? // Thanks for the help! Different futures using different exceptions can solve it. } Now I have a future list like List[Future[T]], and

Is concurrent.futures a medicine of the GIL?

 ̄綄美尐妖づ 提交于 2019-11-29 02:52:20
问题 I was just searching about this new implementation, and i use python 2.7, i must install this, so if i use it, i'll forget the word GIL on CPython? 回答1: No, concurrent.futures has almost nothing whatsoever to do with the GIL. Using processes instead of threads is medicine for the GIL. (Of course, like all medicine, it has side effects. But it works.) The futures module just gives you a simpler way to schedule and wait on tasks than using threading or multiprocessing directly. And it has the

Is it possible to install a callback after request processing is finished in Spray?

痴心易碎 提交于 2019-11-29 02:42:58
I'm trying to serve large temporary files from Spray. I need to delete those files once HTTP request is complete. I could not find a way to do this so far... I'm using code similar to this or this: respondWithMediaType(`text/csv`) { path("somepath" / CsvObjectIdSegment) { id => CsvExporter.export(id) { // loan pattern to provide temp file for this request file => encodeResponse(Gzip) { getFromFile(file) } } } } So essentially it calls getFromFile which completes the route in a Future . The problem is that even if that Future is complete the web request is not complete yet. I tried to write a

what is the advantage of using FutureTask over Callable?

北慕城南 提交于 2019-11-29 00:24:52
问题 There are two approaches to submitting and polling task for result FutureTask futureTask = new FutureTask<String>(callable); Use combination of Callable and Future and submit on ExecutorService . Retrieve result using future.get() . Future future = service.submit(callable); Use FutureTask . This will wrap Callable and then retrieve result using FutureTask . service.execute(task); What is the advantage of using FutureTask over Callable + Future combination ? 回答1: Almost certainly none at all.