future

convert Scala Future to Twitter Future

半世苍凉 提交于 2019-12-09 10:13:58
问题 I use Finagle as a web server which I want to return Scala-Futures from my application logic. How to convert scala.concurrent.Future to com.twitter.util.Future, in a non-blocking way of course? 回答1: Have not enough environment to test this, but here is what i write for "com.twitter" %% "finagle-http" % "6.25.0" : import com.twitter.{util => twitter} import scala.concurrent.{ExecutionContext, Promise, Future} import scala.util.{Failure, Success, Try} import language.implicitConversions object

Why aren't my scala futures more efficient?

一世执手 提交于 2019-12-09 08:33:38
问题 I'm running this scala code on a 32-bit quad-core Core2 system: def job(i:Int,s:Int):Long = { val r=(i to 500000000 by s).map(_.toLong).foldLeft(0L)(_+_) println("Job "+i+" done") r } import scala.actors.Future import scala.actors.Futures._ val JOBS=4 val jobs=(0 until JOBS).toList.map(i=>future {job(i,JOBS)}) println("Running...") val results=jobs.map(f=>f()) println(results.foldLeft(0L)(_+_)) (Yes, I do know there are much more efficient ways to sum a series of integers; it's just to give

How to create a completed future in java

♀尐吖头ヾ 提交于 2019-12-09 07:21:25
问题 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

How to use ExecutorService to poll until a result arrives

一个人想着一个人 提交于 2019-12-09 06:47:56
问题 I have a scenario where I have to poll a remote server checking if a task has completed. Once it has, I make a different call to retrieve the result. I originally figured I should use a SingleThreadScheduledExecutor with scheduleWithFixedDelay for polling: ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); ScheduledFuture future = executor.scheduleWithFixedDelay(() -> poll(jobId), 0, 10, TimeUnit.SECONDS); public void poll(String jobId) { boolean jobDone =

How are Scala Futures chained together with flatMap?

烂漫一生 提交于 2019-12-09 06:02:52
问题 I'm working on using Futures for the first time in Scala and am working through an example of using the flatMap combinator; I've been following this discussion: http://docs.scala-lang.org/overviews/core/futures.html Specifically, this example: val usdQuote = future { connection.getCurrentValue(USD) } val chfQuote = future { connection.getCurrentValue(CHF) } val purchase = for { usd <- usdQuote chf <- chfQuote if isProfitable(usd, chf) } yield connection.buy(amount, chf) purchase onSuccess {

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

北战南征 提交于 2019-12-08 17:31:40
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 => Ok("form") case result => { val createIndexResult: Future[Response] = EsClient.execute(new

Use the results of two Guava ListenableFutures of different types

不问归期 提交于 2019-12-08 16:18:04
问题 I have two ListenableFutures which are completed on other threads. Each future is of a different type, and I wish to use both of their results when they are both complete. Is there an elegant way to handle this using Guava? 回答1: Runnable listener = new Runnable() { private boolean jobDone = false; @Override public synchronized void run() { if (jobDone || !(future1.isDone() && future2.isDone())) { return; } jobDone = true; // TODO do your job } }; future1.addListener(listener); future2

ForkJoinTask vs CompletableFuture

為{幸葍}努か 提交于 2019-12-08 15:59:03
问题 In Java 8 there are two ways of starting asynchronous computations - CompletableFuture and ForkJoinTask . They both seem fairly similar - the inner classes of CompletableFuture even extend ForkJoinTask . Is there a reason to use one over the other? One key difference that I can see is that the CompletableFuture.join method simply blocks until the future is complete ( waitingGet just spins using a ManagedBlocker ), whereas a ForkJoinTask.join can steal work off the queue to help the task you

Why new thread instead of future {…}

浪尽此生 提交于 2019-12-08 15:24:41
问题 This answer instructs how to convert java.util.concurrent.Future into scala.concurrent.Future , while managing where the blocking will occur: import java.util.concurrent.{Future => JFuture} import scala.concurrent.{Future => SFuture} val jfuture: JFuture[T] = ??? val promise = Promise[T]() new Thread( new Runnable { def run() { promise.complete(Try{ jfuture.get }) } } ).start val future = promise.future My queston is the same as a question asked in the comments: what's wrong with future {

Use tornado future to fetch url, two different ways get different results

╄→гoц情女王★ 提交于 2019-12-08 13:13:27
问题 I want to use tornado to fetch batch urls. So my code shows below: from tornado.concurrent import Future from tornado.httpclient import AsyncHTTPClient from tornado.ioloop import IOLoop class BatchHttpClient(object): def __init__(self, urls, timeout=20): self.async_http_client = AsyncHTTPClient() self.urls = urls self.timeout = 20 def __mid(self): results = [] for url in self.urls: future = Future() def f_callback(f1): future.set_result(f1.result()) f = self.async_http_client.fetch(url) f.add