future

blocking keyword in Scala

别说谁变了你拦得住时间么 提交于 2019-12-03 11:46:01
What's the difference between Future(blocking(blockingCall())) and blocking(Future(blockingCall())) ? Both of these are defined in scala.concurrent._ I've looked at the scala docs and some other stack overflow answers but remain unclear on what the difference is. Michael Zajac blocking acts as a hint to the ExecutionContext that it contains blocking code, so that it may spawn a new thread to prevent deadlocks. This presumes the ExecutionContext can do that, but not all are made to. Let's look at each one-by-one. Future(blocking(blockingCall())) This requires an implicit ExecutionContext to

ability to get the progress on a Future<T> object

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 11:43:14
问题 With reference to the java.util.concurrent package and the Future interface I notice (unless I am mistaken) that the ability to start a lengthy tasks and be able to query on the progress only comes with the SwingWorker implementing class. This begs the following question: Is there a way, in a non-GUI, non-Swing application (imaging a console application) to start a lengthy task in the background and allow the other threads to inspect the progress ? It seems to me that there is no reason why

Using Futures in Akka Actors

不羁岁月 提交于 2019-12-03 11:42:32
问题 I'm just starting to learn Akka Actors in Scala. My understanding is that messages received by an Actor are queued in an Actor's mailbox, and processed one at a time. By processing messages one at a time, concurrency issues (race conditions, deadlocks) are mitigated. But what happens if the Actor creates a future to do the work associated with a message? Since the future is async, the Actor could begin processing the next several messages while the future associated with the prior message is

Does the future object returned by executorService.submit(Runnable) hold any reference to the runnable object?

橙三吉。 提交于 2019-12-03 11:38:28
Let's assume we have the following code: List<Future<?>> runningTasks; ExecutorService executor; ... void executeTask(Runnable task){ runningTasks.add(executor.submit(task)); } My questions are: Does runningTasks hold a reference to the task object? How long does it hold it for? Does it still hold it after the task is complete? In order to avoid memory leaks do I have to take care to remove the future that was added to the list? Until when the executor or the Future object holds a reference to it is an implementation detail . Therefore, if your tasks use a lot of memory such that you have to

What are futures?

孤街醉人 提交于 2019-12-03 10:35:45
What are futures? It's something to do with lazy evaluation. Motti There is a Wikipedia article about futures. In short, it's a way to use a value that is not yet known. The value can then be calculated on demand (lazy evaluation) and, optionally, concurrently with the main calculation. C++ example follows. Say you want to calculate the sum of two numbers. You can either have the typical eager implementation: int add(int i, int j) { return i + j; } // first calculate both Nth_prime results then pass them to add int sum = add(Nth_prime(4), Nth_prime(2)); or you can use the futures way using C+

mapping a Stream with a function returning a Future

只谈情不闲聊 提交于 2019-12-03 10:35:38
I sometimes find myself in a situation where I have some Stream[X] , and a function X => Future Y , that I'd like to combine to a Future[Stream[Y]] , and I can't seem to find a way to do it. For example, I have val x = (1 until 10).toStream def toFutureString(value : Integer) = Future(value toString) val result : Future[Stream[String]] = ??? I tried val result = Future.Traverse(x, toFutureString) which gives the correct result, but seems to consume the entire stream before returning the Future, which more or less defeats the purpse I tried val result = x.flatMap(toFutureString) but that doesn

When using Scala futures, will chained callbacks with the same execution context be optimised into synchronous calls?

﹥>﹥吖頭↗ 提交于 2019-12-03 10:29:57
The new Future in Scala 2.10 uses an execution context for every operation where an action is called asynchronously (including map , filter , etc). Does this mean that every action will always be called individually through the execution context, or is it possible that this step is optimized away when chaining multiple transformations/filters each using the same execution context? I.e. if doing f.map(...).filter(...).map(...) , all with the same execution context, will this call execute() once (because it's clever enough to compose a synchronous function from the above), or three times? If the

Python web框架 Tornado异步非阻塞

大憨熊 提交于 2019-12-03 09:52:04
Python web框架 Tornado异步非阻塞 异步非阻塞 阻塞式:(适用于所有框架,Django,Flask,Tornado,Bottle)   一个请求到来未处理完成,后续一直等待   解决方案:多线程,多进程 异步非阻塞(存在IO请求): Tornado(单进程+单线程)   使用异步非阻塞,需要遵循Tornado框架内部规则,gen   多个连接请求,连接给服务端,如果是有异步非阻塞的话,服务端会接收所有的请求交由后台处理,等待其他链接的同时,原先连接不断开,直至返回后台处理完成的结果!   外部请求,连接服务端 或在select中创建Future对象,然后服务端再把请求交给业务处理平台,此时select监听的列表中又会生成一个socket对象,当业务平台对请求处理完成之后就会把信息返回到服务端的select监听列表中,同时对这个Future对象赋值,用于标记服务端是否要给客户端返回请求信息。   执行流程,本质上都是返回一个future对象,如果对这个对象被set_result了就返回值,否则就是夯住,一直保持连接,不终止请求。 1、基本使用 装饰器 + Future 从而实现Tornado的异步非阻塞 class AsyncHandler(tornado.web.RequestHandler): @gen .coroutine def get( self ):

Error handling Scala : Future For Comprehension

旧时模样 提交于 2019-12-03 09:36:20
问题 I want to do error handling in my play scala web application. My application talks to the data base to fetch some rows, it follows following flow. First call to db to fetch some data Use the data in first call to fetch other data from db Form a response using the data received from last two db calls. Below is my pseudocode. def getResponse(name: String) (implicit ctxt: ExecutionContext): Future[Response] = { for { future1 <- callFuture1(name) future2 <- callFuture2(future1.data) future3 <-

How to create a completed future in java

余生颓废 提交于 2019-12-03 09:16:54
What is the best way to construct a completed future in Java? I have implemented my own CompletedFuture below, but was hoping something like this that already exists. public class CompletedFuture<T> implements Future<T> { private final T result; public CompletedFuture(final T result) { this.result = result; } @Override public boolean cancel(final boolean b) { return false; } @Override public boolean isCancelled() { return false; } @Override public boolean isDone() { return true; } @Override public T get() throws InterruptedException, ExecutionException { return this.result; } @Override public