future

How to convert Map[A,Future[B]] to Future[Map[A,B]]?

戏子无情 提交于 2019-11-28 23:53:30
问题 I've been working with the Scala Akka library and have come across a bit of a problem. As the title says, I need to convert Map[A, Future[B]] to Future[Map[A,B]] . I know that one can use Future.sequence for Iterables like Lists, but that doesn't work in this case. I was wondering: is there a clean way in Scala to make this conversion? 回答1: See if this works for you: val map = Map("a" -> future{1}, "b" -> future{2}, "c" -> future{3}) val fut = Future.sequence(map.map(entry => entry._2.map(i =

How can I make A future of future into one future object?

若如初见. 提交于 2019-11-28 21:23:09
Env: Akka 2.1, scala version 2.10.M6, JDK 1.7,u5 Now is my problem: I have: future1 = Futures.future(new Callable<Future<object>>(){...}); future2 = ? extends Object; Future.sequence(future1, future2).onComplete(...) now in first line, I have a future of Future of object, is there any way to convert it into a Future while not blocking my current thread? Is there any method in akka? As far as I checked, I havn't found any yet... First time to have a post....Sry for bad format and organize... :~P Short answer (English): flatMap dat sh!t Shorter answer (Scala): flatMap(identity) Shortest answer:

Pass multiple parameters to concurrent.futures.Executor.map?

岁酱吖の 提交于 2019-11-28 20:08:29
The concurrent.futures.Executor.map takes a variable number of iterables from which the function given is called. How should I call it if I have a generator that produces tuples that are normally unpacked in place? The following doesn't work because each of the generated tuples is given as a different argument to map: args = ((a, b) for (a, b) in c) for result in executor.map(f, *args): pass Without the generator, the desired arguments to map might look like this: executor.map( f, (i[0] for i in args), (i[1] for i in args), ..., (i[N] for i in args), ) vz0 You need to remove the * on the map

Futures - map vs flatmap

冷暖自知 提交于 2019-11-28 19:55:58
问题 I've read the docs about map and flatMap and I understand that flatMap is used for an operation that accepts a Future parameter and returns another Future . What I don't fully understand is why I would want to do this. Take this example: User hits my webservice asking to "do stuff" I download a file (which is slow) I process the file (which is CPU intensive) Render the result I understand that I would want to use a future to download the file but I have have two options re processing it: val

Unable to use for comprehension to map over List within Future

不羁的心 提交于 2019-11-28 18:34:38
I have this issue that I have to work around every time. I can't map over something that is contained within a Future using a for comprehension. Example: import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future val f = Future( List("A", "B", "C") ) for { list <- f e <- list } yield (e -> 1) This gives me the error: error: type mismatch; found : List[(String, Int)] required: scala.concurrent.Future[?] e <- list ^ But if I do this it works fine: f.map( _.map( (_ -> 1) ) ) Should i not be able to do this by using a for comprehension, is the reason it works in my

Utility of Future.cancel(boolean) method

谁说胖子不能爱 提交于 2019-11-28 18:32:58
问题 I was simply exploring the java.util.concurrent package. I learnt that the class ' Future ' has a method boolean cancel(boolean mayInterruptIfRunning) Please find attached the test code I wrote : package com.java.util.concurrent; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; public class FutureTester { /** * @param args * @throws InterruptedException */ public

Confusion about threads launched by std::async with std::launch::async parameter

跟風遠走 提交于 2019-11-28 18:14:53
I am a little bit confused about the std::async function. The specification says: asynchronous operation being executed "as if in a new thread of execution" (C++11 §30.6.8/11). Now, what does that supposed to mean? In my understanding, the code std::future<double> fut = std::async(std::launch::async, pow2, num); should launch the function pow2 on a new thread and pass the variable num to the thread by value, then sometime in the future, when the function is done, place the result in fut (as long as the function pow2 has a signature like double pow2(double); ). But the specification states "as

Scala - ScheduledFuture

拥有回忆 提交于 2019-11-28 17:51:05
I am trying to implement scheduled future in Scala. I would like it to wait specific time and then execute the body. So far I tried the following, simple approach val d = 5.seconds.fromNow val f = future {Await.ready(Promise().future, d.timeLeft); 1} val res = Await.result(f, Duration.Inf) but I am getting the TimeoutExcpetion on the future. Is this even the correct approach or should I simply use the ScheduledExecutor from Java? You could change your code to something like this: val d = 5.seconds.fromNow val f = Future {delay(d); 1} val res = Await.result(f, Duration.Inf) def delay(dur

Promise equivalent in C#

≯℡__Kan透↙ 提交于 2019-11-28 15:45:13
问题 In Scala there is a Promise class that could be used to complete a Future manually. I am looking for an alternative in C#. I am writing a test and I want it to look it similar to this: // var MyResult has a field `Header` var promise = new Promise<MyResult>; handlerMyEventsWithHandler( msg => promise.Complete(msg); ); // Wait for 2 seconds var myResult = promise.Future.Await(2000); Assert.Equals("my header", myResult.Header); I understand that this is probably not the right pattern for C#,

FutureTask在缓存中使用

好久不见. 提交于 2019-11-28 15:22:54
缓存主要作用是提高应用程序吞吐量和响应性,当然也有负面影响,占用更多内存。在设计 SqlTemplate 也有个简单的本地缓存,sql模板实际只需要解释一次就可以了,以后的调用复用之前解释过。开始的时候是使用简单的HashMap实现的,但是在并发情况下会出现重复解释,下面是第一版的代码片段。 public class Configuration { private ConcurrentHashMap<String, SqlTemplate > templateCache; ...... public SqlTemplate getTemplate(final String content) { if (cacheTemplate) { //是 否则缓存模板 SqlTemplate sqlTemplate = templateCache.get(content); if (sqlTemplate != null){ sqlTemplate = createTemplate(content) ; templateCache.put(content, sqlTemplate) ; } return sqlTemplate; } return createTemplate(content); } //解释构建模板对象 private SqlTemplate createTemplate