future

Asynchronous Iterable over remote data

≡放荡痞女 提交于 2019-12-11 12:13:04
问题 There is some data that I have pulled from a remote API, for which I use a Future-style interface. The data is structured as a linked-list. A relevant example data container is shown below. case class Data(information: Int) { def hasNext: Boolean = ??? // Implemented def next: Future[Data] = ??? // Implemented } Now I'm interested in adding some functionality to the data class, such as map , foreach , reduce , etc. To do so I want to implement some form of IterableLike such that it inherets

scala futures - keeping track of request context when threadId is irrelevant

烂漫一生 提交于 2019-12-11 10:26:26
问题 making first steps using Scala promises/futures in a web server (still Jetty/synchronous for now :( ), as expected the threadId is no longer useful for tracking what happened during a single HTTP request processing. I guess logging a request token will do the trick - is this the recommended approach? if so can you refer me to some code already doing that? 回答1: My approach in such situation is to have a case class that holds contextual data (such as request id) and pass it throught all layers

Netty ChannelFuture timeout when response received

旧城冷巷雨未停 提交于 2019-12-11 06:08:13
问题 I am new in netty I have a tcp client application developed with netty. When i use future get async response from server some response returning but future is not completing into timeout. TCPClient class like following; public TcpClient { public boolean connect(Host host) { try { Bootstrap clientBootstrap = new Bootstrap() .group(group) .channel(NioSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE,true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 50) .remoteAddress(new

Does a Future instantly return false for isDone() right after an executor submit()

Deadly 提交于 2019-12-11 04:12:15
问题 Could not find a documentation about the future state of an executor.submit() call. Example: byte[] b = new byte[ 4000000 ]; new Random().nextBytes( b ); Callable<byte[]> c = new SorterCallable( b ); ExecutorService executor = Executors.newCachedThreadPool(); Future<byte[]> result = executor.submit( c ); boolean futureState = result.isDone(); The question is if the returned Future<byte[]> object returns false when calling isDone() right after submit? Or is it also possible that futureState is

akka getSender() lost in future

前提是你 提交于 2019-12-11 02:54:37
问题 I have a problem in akka (java) with the reference of the sender which disappears after a while in a future. Here is the code: class MyActor extends UntypedActor { @Override public void onReceive(Object msg){ Future<Integer> future = Futures.future(new Callable<Integer>(){ @Override public Integer call() throws Exception { System.out.println(getSender()); //works fine Thread.sleep(1000); System.out.println(getSender()); //show deadLetter return 42; } },getContext().dispatcher()); //do

How do I add tasks to a Tokio event loop that is running on another thread?

*爱你&永不变心* 提交于 2019-12-11 02:44:34
问题 I'd like to spin up a Tokio event loop alongside a Rocket server, then add events to this loop later on. I read Is there a way to launch a tokio::Delay on a new thread to allow the main loop to continue?, but it's still not clear to me how to achieve my goal. 回答1: As the documentation states: While current_thread::Runtime does not implement Send and cannot safely be moved to other threads, it provides a Handle that can be sent to other threads and allows to spawn new tasks from there. Here is

Scala actor to non-actor interaction (or synchronizing messages from an actor to a servlet)

浪子不回头ぞ 提交于 2019-12-11 02:14:09
问题 I have the following scala code: package dummy import javax.servlet.http.{HttpServlet, HttpServletRequest => HSReq, HttpServletResponse => HSResp} import scala.actors.Actor class DummyServlet extends HttpServlet { RNG.start override def doGet(req: HSReq, resp: HSResp) = { def message = <HTML><HEAD><TITLE>RandomNumber </TITLE></HEAD><BODY> Random number = {getRandom}</BODY></HTML> resp.getWriter().print(message) def getRandom: String = {var d = new DummyActor;d.start;d.getRandom} } class

direct use of Futures in Akka

时光总嘲笑我的痴心妄想 提交于 2019-12-11 01:55:36
问题 I am not able to create a Future as explained here. It says you can create a Future directly using the following code: import akka.dispatch.Await import akka.dispatch.Future import akka.util.duration._ val future = Future { "Hello" + "World" } val result = Await.result(future, 1 second) Using the exact same code, I get an errormessage, saying: error: could not find implicit value for parameter executor: akka.dispatch.ExecutionContext . All I can find about the ExecutionContext is, that you

Implement search using futures, async, and thread in C++11

試著忘記壹切 提交于 2019-12-10 20:08:14
问题 I would like to implement branch and bound search in a multithreaded manner. In particular, I want to use async to wrap the search calls at each branch, then just wait until some thread comes up with the answer, and just exit. (Ideally, I would want to cancel the other threads, but thread cancelling is not in the standard). Here is some simplified code : #include <iostream> #include <random> #include <future> #include <thread> using namespace std; mt19937 rng; uniform_int_distribution

How to compose Future of Either/Disjunction in Scala

岁酱吖の 提交于 2019-12-10 18:46:03
问题 Suppose I have the following functions to compose: val mayFail1: Int => Error \/ Int = ??? val slowAndMayFail: Int => Error \/ String = ??? val mayFail2: String => Error \/ Int = ??? val mayFail3: String => Error \/ Int = ??? val mayFail: Int => Error \/ Int = {x => for { x1 <- mayFail1(x) s <- slowAndMayFail(x1) x2 <- mayFail2(s) x3 <- mayFail3(x2) } yield x3 } The function mayFail is slow because of slowAndMayFail , so I would like it to return a future of Error \/ Int . The straightforward