future

Scala futures: what is the main thread expected to do while futures are executing?

与世无争的帅哥 提交于 2020-01-04 02:49:07
问题 I (a newbie) am testing my concepts about Scala's Futures and the right patterns to use them. The premise Scala's futures are blocks of code to be executed asynchronously. So, the main thread creates one or more such futures, installs onSuccess() [note: equally applicable to OnComplete/onFailure] callbacks and proceeds. The callbacks are executed as and when the futures complete their runs. Presumably, these callbacks generate results which are supposed to be used by the main thread. The

Futures and Promises in Erlang

懵懂的女人 提交于 2020-01-03 17:24:18
问题 Does Erlang has equivalents for Future and Promises? Or since future and promises are solving a problem that doesn't exist in Erlang systems (synchronisation orchestrating for example), and hence we don't need them in Erlang. If I want the semantics of futures and promises in Erlang, they could be emulated via Erlang processes/actors? 回答1: You could easily implement a future in Erlang like this: F = fun() -> fancy_function() end, % fancy code Pid = self(), Other = spawn(fun() -> X = F(), Pid

Scala Play Action.async cant resolve Ok as mvc.AnyContent

自闭症网瘾萝莉.ら 提交于 2020-01-03 02:46:10
问题 The following controller action cause the error: (block: => scala.concurrent.Future[play.api.mvc.SimpleResult])play.api.mvc.Action[play.api.mvc.AnyContent] cannot be applied to (scala.concurrent.Future[Object])". Every Future inside the action should be Ok() so I don't understand why scala can't resolve the futures correctly. def form = Action.async { val checkSetup: Future[Response] = EsClient.execute(new IndexExistsQuery("fbl_indices")) checkSetup map { case result if result.status == 200 =

Difference between Await.result and futures.onComplete in Scala

一笑奈何 提交于 2020-01-02 10:29:09
问题 I am using the following two code snippets to execute code in multiple threads. But I am getting different behaviour. Snippet 1: val futures = Future.sequence(Seq(f1, f2, f3, f4, f5)) futures.onComplete{ case Success(value) => case Failure(value) => } Snippet 2: Await.result(Future.sequence(Seq(f1, f2, f3, f4, f5)), Duration(500, TimeUnit.SECONDS)) In futures I am just setting some property and retrieving the result. Note: knowing only the behaviour difference between above two snippets is

Why Scala for comprehension run Future functions sequentially?

人盡茶涼 提交于 2020-01-02 09:36:22
问题 Consider the following code: import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Await import scala.concurrent.duration._ object FutureFor { def getA(n: Int) = { val x: Future[String] = Future { println("I'm getA") for (i <- 1 to 5) { println(".") Thread.sleep(200) } s"A$n" } x } def getB(n: Int) = { val x: Future[String] = Future { println("I'm getB") for (i <- 1 to 5) { println(".") Thread.sleep(200) } s"B$n" } x } def main(args:

Turning a listener into a future in java

谁说胖子不能爱 提交于 2020-01-02 04:01:08
问题 I'm trying to turn a listener into a Future, for asynchronous connection. I'm not used to using java futures yet, I've some experience with javascript promises but I fail to see how to write it in java (I've seen "CompletableFuture" in Java 8 may solve my problem, unfortunately I'm stuck with java 7). Here's what I've done so far: public Future<Boolean> checkEmailClientConfiguration(final EmailClientConfiguration config) { final Future<Boolean> future = ???; // In some other languages I would

Why does `Future#toString` returns `“List()”`?

故事扮演 提交于 2020-01-01 09:42:21
问题 Calling .toString on a future without waiting for complession leads to nondeterministic results. My question is why calling .toString on uncompleted futures returns "List()" in scala 2.10.x and 2.11.x? The implementation does not seem to be explicit about that. This behavior can be observed from the REPL: scala> import scala.concurrent.Future, scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.ExecutionContext.Implicits.global scala> Future(1).toString res0: scala

future.isDone returns false even if the task is done

瘦欲@ 提交于 2020-01-01 05:37:08
问题 I have tricky situation, Does future.isDone() returns false , even if the thread is done. import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class DataAccessor {

Flutter: shared preferences

馋奶兔 提交于 2020-01-01 04:43:09
问题 I have this function: Future<String> load(SharedPreferences prefs, String fileName) async { prefs = await SharedPreferences.getInstance(); String jsonString = prefs.getString(fileName) ?? ""; if (jsonString.isNotEmpty) { return jsonString; }else{ return ... } } What should I return in the else case? I tried with "" but it doesn't work. 回答1: The answer is "it depends". Namely, it depends on what exactly you are doing with the result of this function, and what a good empty default value means

Akka Actor ask and Type Safety

随声附和 提交于 2019-12-31 19:38:48
问题 How can I use Akka Actor ask and maintain type safety? or avoid using ask in favour of tells? When calling ? or ask on an Akka Actor, a Future[Any] is returned and I have to do an explicit cast via future.mapTo[MyType] . I don't like losing this type safety. If I use Futures directly (with no actors) I can explicitly return Future[MyType] and maintain type safety. My specific use case involves an actor delegating it's message to two child actors and then aggregating the results from those