future

Scala futures creating infinity threads in actors

℡╲_俬逩灬. 提交于 2019-12-25 07:58:32
问题 I am using futures and timeouts inside an actor to avoid system to wait to long for a command to execute: val _output = future { "myCommand.sh" !! } output = try { Await.result(_output, 3 minutes) } catch { case e: TimeoutException => { LOG.warn(s"Timeout for command. Will return empty.") "" } } System.out.println("Number of active threads from the given thread: " + Thread.activeCount()); I run this code hundreds of times from different actors. I am noticing that the number of running threads

Scala futures creating infinity threads in actors

拟墨画扇 提交于 2019-12-25 07:57:25
问题 I am using futures and timeouts inside an actor to avoid system to wait to long for a command to execute: val _output = future { "myCommand.sh" !! } output = try { Await.result(_output, 3 minutes) } catch { case e: TimeoutException => { LOG.warn(s"Timeout for command. Will return empty.") "" } } System.out.println("Number of active threads from the given thread: " + Thread.activeCount()); I run this code hundreds of times from different actors. I am noticing that the number of running threads

Scala Play Future Interdependancy

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 07:15:54
问题 I am using play-slick for my scala Play! dummy rest API. So, I have to fetch records from multiple tables. But, they are interdependent, i.e. Table_1 Table_2 id1 id2 id2 To fetch a record from Table_2 I have to fetch a record from Table_1 and then using id2 fetch from Table_2. My controller: def getEntity(id : Long) = Action.async { table1DAO.findById(id) map { t1 => t1 map { t1Entity => table2DAO.findById(t1Entity.id2) map { t2 => t2 map { t2Entity => Ok(Json.toJson(CombiningClass(t1Entity,

The concurrent.blocking in future not work as expected in some scenario

半世苍凉 提交于 2019-12-25 01:43:58
问题 Help to explain 2 phenomenon for scala future(Bold in code4 & code5), thanks. Code1 package com.tst import akka.actor.{Actor, ActorSystem, Props} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future class MyActor extends Actor { def receive = { case _ => for (i <- 1 to 6) { Future { println("start") Thread.sleep(30000) println("end") } } } } object Test extends App { val system = ActorSystem() val myActor = system.actorOf(Props[MyActor]) myActor ! 'msg }

Handling unexpected exceptions on scala Futures

﹥>﹥吖頭↗ 提交于 2019-12-24 17:44:13
问题 When using some code like the following: scala> Future { null } onComplete { case Success(v) => v.toString } Scala throws the following exception: scala> java.lang.NullPointerException at $line14.$read$$iw$$iw$$anonfun$2.apply(<console>:11) at $line14.$read$$iw$$iw$$anonfun$2.apply(<console>:11) at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) at scala.concurrent.impl.ExecutionContextImpl$$anon$3.exec(ExecutionContextImpl.scala:107) at scala.concurrent.forkjoin.ForkJoinTask

Handling futures with for-comp, but if clauses are making things difficult

青春壹個敷衍的年華 提交于 2019-12-24 16:30:00
问题 I find that I am running into the below pattern allot and I can't get it to work or look nice style wise. I have a for comprehension that is return Futures, and then I build my model to display in a view. But before I return the Result in my Action, I sometimes have to branch using an if clause and potentially load more data. The below code doesn't compile currently, what do you suggest I do to make the below code follow correct style with this type of pattern? It doesn't compile because the

Appending a global table using asynchronous futures in R

吃可爱长大的小学妹 提交于 2019-12-24 10:47:58
问题 I am trying to create a global table that is produced by asynchronously running parallel processes. They are completely independent, however they should append to the same global variable (this is reactive in R shiny so I either need to have a call back function once all futures are done with their task -which would be very nice but I dont know how-, or I need to constantly update the table as new results come in). I tried the following approach which just locks (probably because all

How do I write a futures::Stream to disk without storing it entirely in memory first?

℡╲_俬逩灬. 提交于 2019-12-24 08:25:39
问题 There's an example of downloading a file with Rusoto S3 here: How to save a file downloaded from S3 with Rusoto to my hard drive? The problem is that it looks like it's downloading the whole file into memory and then writing it to disk, because it uses the write_all method which takes an array of bytes, not a stream. How can I use the StreamingBody, which implements futures::Stream to stream the file to disk? 回答1: Since StreamingBody implements Stream<Item = Vec<u8>, Error = Error> , we can

How to properly submit and get several Futures in the same Java stream?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 06:38:33
问题 I try to submit and get 10 Future s in the same stream. Each one takes 1 second to process and I would like to run them in parallel. My first try is takes_10_sec() which runs sequentially and takes 10s. My second try is takes_1_sec() which runs in parallel and takes 1s. However it uses an intermediate .collect(Collectors.toList()).stream() which I don't think is a good way to do it. Is there another recommended way? public class FutureStream { private ExecutorService executor = Executors

What is the difference between `then`, `and_then` and `or_else` in Rust futures?

不羁岁月 提交于 2019-12-24 05:34:06
问题 I am learning to use Rust futures and I am finding it extremely confusing. I feel like I am being stupid but when would then , and_then and or_else be used? What return types are expected? Please provide some examples of the different situations you would expect to see them. 回答1: TL;DR: then is used when you want to do something regardless of if the future was successful or not, and_then runs the closure only when the future succeeded, and or_else runs the closure only when the future failed.