future

Await a future, receive an either

一个人想着一个人 提交于 2019-12-18 12:47:26
问题 I'd like to await a scala future that may have failed. If I use Await.result the exception will be thrown. Instead, if I have f: Future[String] I would like a method Await.resultOpt(f): Option[String] or Await.resultEither(f): Either[String] . I could get this by using scala.util.control.Exception.catching or I could f map (Right(_)) recover { case t: Throwable => Left(t) } , but there must be a more straightforward way. 回答1: You could use Await.ready which simply blocks until the Future has

Is there any way to asynchronously wait for a future in Boost Asio?

旧街凉风 提交于 2019-12-18 11:48:16
问题 My problem is the following. I start several operations asynchronously, and I want to continue until all of them are finished. Using Boost Asio, the most straightforward way to do this is the following. Suppose tasks is some kind of container of objects that support some asynchronous operation. tasksToGo = tasks.size(); for (auto task: tasks) { task.async_do_something([](const boost::system::error_code& ec) { if (ec) { // handle error } else { if (--taslsToGo == 0) { tasksFinished(); } } });

How do I wait for asynchronous tasks to complete in scala?

北战南征 提交于 2019-12-18 10:44:22
问题 I'm aware that my problem might seem a little bit complex. But I'll try to express myself well. I have this method which I want to return a Map[String, List[String]] filled with data. def myFunction():Map[String, List[String]] = { val userMap = Map[String, String](("123456", "ASDBYYBAYGS456789"), ("54321", "HGFDSA5432")) //the result map to return when all data is collected and added val resultMap:Future[Map[String, List[String]]] //when this map is finished (filled) this map is set to

Correct Implementation of Java Future multithreading

纵饮孤独 提交于 2019-12-18 09:08:41
问题 In my servlet I am hitting several URL's to check their status and returning the response to user. Hitting multipe requests takes lot of time: need threads and timouts. But i need my threads to geturn response : using Future for that reason. My code outline: ExecutorService executor = Executors.newFixedThreadPool(10); Future<statusModel> future; for (Map.Entry<String, String> url : urls.entrySet()) { try { future = executor.submit(new CallableRequestStatus(url.getValue())); status =

Execution context without daemon threads for futures

匆匆过客 提交于 2019-12-18 06:58:14
问题 I am having trouble with the JVM immediately exiting using various new applications I wrote which spawn threads through the Scala 2.10 Futures + Promises framework. It seems that at least with the default execution context, even if I'm using blocking, e.g. future { blocking { /* work */ }} no non-daemon thread is launched, and therefore the JVM thinks it can immediately quit. A stupid work around is to launch a dummy Thread instance which is just waiting, but then I also need to make sure

Futures / Success race

房东的猫 提交于 2019-12-17 22:59:53
问题 I'm learning futures, and I'm trying to create a method that, take two futures as parameter ( f and g ) and return the first future that was successfully completed, otherwise it returns f or g . Some use cases to illustrate the behaviour of my method are : Future 1 | Future 2 | Result Success First Success Second Future 1 Success First Failure Second Future 1 Success Second Success First Future 2 Success Second Failure First Future 1 Failure First Failure Second Future 2 (because we had a

unreported exception when throwing from a lambda in a Completable Future

做~自己de王妃 提交于 2019-12-17 21:24:09
问题 When I compile the code below, I get the following error: /home/prakashs/composite_indexes/src/main/java/com/spakai/composite/TwoKeyLookup.java:22: error: unreported exception NoMatchException; must be caught or declared to be thrown CompletableFuture<Set<V>> result = calling.thenCombine(called, (s1, s2) -> findCommonMatch(s1, s2)); The code: public CompletableFuture<Set<V>> lookup(K callingNumber, K calledNumber) throws NoMatchException { CompletableFuture<Set<V>> calling =

Combine awaitables like Promise.all

落花浮王杯 提交于 2019-12-17 15:38:18
问题 In asynchronous JavaScript, it is easy to run tasks in parallel and wait for all of them to complete using Promise.all : async function bar(i) { console.log('started', i); await delay(1000); console.log('finished', i); } async function foo() { await Promise.all([bar(1), bar(2)]); } // This works too: async function my_all(promises) { for (let p of promises) await p; } async function foo() { await my_all([bar(1), bar(2), bar(3)]); } I tried to rewrite the latter in python: import asyncio async

Scala's “for comprehension” with futures

一世执手 提交于 2019-12-17 07:03:11
问题 I am reading through the Scala Cookbook (http://shop.oreilly.com/product/0636920026914.do) There is an example related to Future use that involves for comprehension. So far my understanding about for comprehension is when use with a collection it will produce another collection with the same type. For example, if each futureX is of type Future[Int] , the following should also be of type Future[Int] : for { r1 <- future1 r2 <- future2 r3 <- future3 } yield (r1+r2+r3) Could someone explain me

Scala's “for comprehension” with futures

社会主义新天地 提交于 2019-12-17 07:03:10
问题 I am reading through the Scala Cookbook (http://shop.oreilly.com/product/0636920026914.do) There is an example related to Future use that involves for comprehension. So far my understanding about for comprehension is when use with a collection it will produce another collection with the same type. For example, if each futureX is of type Future[Int] , the following should also be of type Future[Int] : for { r1 <- future1 r2 <- future2 r3 <- future3 } yield (r1+r2+r3) Could someone explain me