future

How to properly deal with exceptions coming from ListenableFuture guava?

血红的双手。 提交于 2019-12-31 02:45:08
问题 I have a library in which I have provided two methods, sync and async for our customer. They can call whichever method they feel is right for their purpose. executeSynchronous() - waits until I have a result, returns the result. executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed. They will pass DataKey object which has the user id in it. And we will figure out which machine to call basis on the user id. So we will make http call

How to implement Future as Applicative in Scala?

只愿长相守 提交于 2019-12-30 09:05:31
问题 Suppose I need to run two concurrent computations, wait for both of them, and then combine their results. More specifically, I need to run f1: X1 => Y1 and f2: X2 => Y2 concurrently and then call f: (Y1, Y2) => Y to finally get a value of Y . I can create future computations fut1: X1 => Future[Y1] and fut2: X2 => Future[Y2] and then compose them to get fut: (X1, X2) => Future[Y] using monadic composition. The problem is that monadic composition implies sequential wait . In our case it implies

Initializing an actor before being able to handle some other messages

流过昼夜 提交于 2019-12-30 08:33:11
问题 I have an actor which creates another one: class MyActor1 extends Actor { val a2 = system actorOf Props(new MyActor(123)) } The second actor must initialize (bootstrap) itself once it created and only after that it must be able to do other job. class MyActor2(a: Int) extends Actor { //initialized (bootstrapped) itself, potentially a long operation //how? val initValue = // get from a server //handle incoming messages def receive = { case "job1" => // do some job but after it's initialized

Flutter - Future builder: when should I use it

故事扮演 提交于 2019-12-30 04:01:04
问题 I was wondering when I should use the future builder. For example, if I want to make an http request and show the results in a list view, as soon as you open the view, should I have to use the future builder or just build a ListViewBuilder like: new ListView.builder( itemCount: _features.length, itemBuilder: (BuildContext context, int position) { ...stuff here... } Moreover, if I don't want to build a list view but some more complex stuff like circular charts, should I have to use the future

When is it safe to move a member value out of a pinned future?

一个人想着一个人 提交于 2019-12-30 03:42:06
问题 I'm writing a future combinator that needs to consume a value that it was provided with. With futures 0.1, Future::poll took self: &mut Self , which effectively meant that my combinator contained an Option and I called Option::take on it when the underlying future resolves. The Future::poll method in the standard library takes self: Pin<&mut Self> instead, so I've been reading about the guarantees required in order to safely make use of Pin . From the pin module documentation on the Drop

Scala-way to handle conditions in for-comprehensions?

我与影子孤独终老i 提交于 2019-12-30 03:25:18
问题 I am trying to create a neat construction with for-comprehension for business logic built on futures. Here is a sample which contains a working example based on Exception handling: (for { // find the user by id, findUser(id) returns Future[Option[User]] userOpt <- userDao.findUser(userId) _ = if (!userOpt.isDefined) throw new EntityNotFoundException(classOf[User], userId) user = userOpt.get // authenticate it, authenticate(user) returns Future[AuthResult] authResult <- userDao.authenticate

Must you join on a Thread to ensure its computation is complete

放肆的年华 提交于 2019-12-29 07:05:10
问题 I have a utility method (used for unit testing, it so happens) that executes a Runnable in another thread. It starts the thread running, but does not wait for the Thread to finish, instead relying on a Future . A caller of the method is expected to get() that Future . But is that enough to ensure safe publication of the computation done by the Runnable ? Here is the method: private static Future<Void> runInOtherThread(final CountDownLatch ready, final Runnable operation) { final

No method named `poll` found for a type that implements `Future`

大憨熊 提交于 2019-12-29 01:37:12
问题 I am attempting to create a struct that will allow someone to call .shutdown() , which will resolve a future (that is otherwise pending). It can only be called once. In the implementation of the Future trait, I receive an error that poll is not defined, despite it being present in the documentation (under impl Future ). Though I am using std::future::Future as the impl , I tried adding use futures::prelude::* , which would bring the preview trait into scope. Both RLS and rustc inform me that

Asynchronous IO in Scala with futures

只愿长相守 提交于 2019-12-27 16:25:54
问题 Let's say I'm getting a (potentially big) list of images to download from some URLs. I'm using Scala, so what I would do is : import scala.actors.Futures._ // Retrieve URLs from somewhere val urls: List[String] = ... // Download image (blocking operation) val fimages: List[Future[...]] = urls.map (url => future { download url }) // Do something (display) when complete fimages.foreach (_.foreach (display _)) I'm a bit new to Scala, so this still looks a little like magic to me : Is this the

Why is my Future implementation blocked after it is polled once and NotReady?

让人想犯罪 __ 提交于 2019-12-25 18:58:33
问题 I implemented the future and made a request of it, but it blocked my curl and the log shows that poll was only invoked once. Did I implement anything wrong? use failure::{format_err, Error}; use futures::{future, Async}; use hyper::rt::Future; use hyper::service::{service_fn, service_fn_ok}; use hyper::{Body, Method, Request, Response, Server, StatusCode}; use log::{debug, error, info}; use std::{ sync::{Arc, Mutex}, task::Waker, thread, }; pub struct TimerFuture { shared_state: Arc<Mutex