future

Get rid of Scala Future nesting

天涯浪子 提交于 2019-12-20 08:30:00
问题 Again and again I am struggling when a function relies on some future results. This usually boils down to a result like Future[Seq[Future[MyObject]]] To get rid of that I now use Await inside a helper function to get a non-future object out and reduce the nesting. It looks like this def findAll(page: Int, perPage: Int): Future[Seq[Idea]] = { val ideas: Future[Seq[Idea]] = collection.find(Json.obj()) // [...] ideas.map(_.map { // UGLY? idea => { // THIS RETURNED A Future[JsObject] before val

Why set the interrupt bit in a Callable

眉间皱痕 提交于 2019-12-20 05:59:05
问题 So, this resource (http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html) suggest to set the interrupt bit in a Thread when that Thread does not deal with the interrupt itself, " so that code higher up on the call stack can learn of the interruption and respond to it if it wants to ." Let's say I'm using an ExecutorService to run something in a different Thread. I construct a Callable and pass this Callable into ExecutorService.submit(), which returns a Future. If the Callable

std::async function running serially

淺唱寂寞╮ 提交于 2019-12-20 04:28:07
问题 When using std::async with launch::async in a for loop, my code runs serially in the same thread, as if each async call waits for the previous before launching. In the notes for std::async references (std::async), this is possible if the std::future is not bound to a reference, but that's not the case with my code. Can anyone figure out why it's running serially? Here is my code snippet: class __DownloadItem__ { //DownloadItem is just a "typedef shared_ptr<__DownloadItem__> DownloadItem" std:

boost::future and continuations - value set, but future still blocks

淺唱寂寞╮ 提交于 2019-12-20 03:34:08
问题 I am trying to make the following continuation work - but f.get() blocks. Whats wrong? #include <iostream> #define BOOST_THREAD_PROVIDES_FUTURE #define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION #include <boost/thread/future.hpp> struct Foo { boost::future<int> start() { return p.get_future(); } void finish() { p.set_value(23); } boost::promise<int> p; }; int main () { Foo foo; foo.start().then([](boost::future<int> f) { std::cout << "done:" << std::endl; std::cout << f.get() << std::endl; });

Is it ok to destroy a std::promise before future.get() is called?

喜你入骨 提交于 2019-12-19 17:39:32
问题 I was wondering if it is ok to call promise.get_future(), move that future somewhere else (e.g. into a vector) and possibly let the promise die before even the future.get() is called. In the following example, the call gateway->refreshWithCallback executes the lambda in a thread so that the shared pointer may set the promise free even if in the second loop the future.get() was not called yet, which seems to work but I am angsty! std::vector<std::future<bool>> futures; for(GuiGateway *gateway

Unable to render “loading” using Shiny and futures

荒凉一梦 提交于 2019-12-19 11:29:27
问题 I am trying to use futures to have a "loading" icon appear. This is the code I have library(shiny) library(promises) library(future) plan(multiprocess) disksUI <- function(id) { ns <- NS(id) fluidRow( box( uiOutput(ns("loading")), dataTableOutput(ns("filelist")), width=12 ) ) } disksServer <- function(input, output, session) { state <- reactiveValues(onLoading=FALSE) observe({ if (state$onLoading) { output$loading <- renderUI("Loading") } else { output$loading <- renderUI("Done") } })

How to use the Guava ListenableFuture and the Futures.chain() methods

谁说我不能喝 提交于 2019-12-19 09:57:50
问题 I have a homework task that requires me to learn how to use the Guava concurrency library. In the task I have several thread pools, where each one is controlled by an individual object. Each pool has several working threads that perform simple tasks (mostly emulating doing stuff by using Thread.sleep(long) ) and all those simple tasks are stored in a container object that emulates a messageboard. Each simple task has a dependency list of other tasks, and it cannot be executed until all of

Using loops with Futures in Dart

╄→尐↘猪︶ㄣ 提交于 2019-12-19 06:47:07
问题 Okay, so I have a List of Files and I need to run a function on each member of the list. I essentially want to do something like this: for(File file in files) { functionThatReturnsAFuture(file); } But obviously this won't work, since the function that returns a Future fires of asynchronously. Is my only option something like this? List<File> files = new List<File>(); // Add files somewhere Future processFile(int i) { return new Future.sync(() { //Do stuff to the file if(files.length>i+1) {

Getting data out of a Future in Scala

喜欢而已 提交于 2019-12-19 04:16:07
问题 I've a Future[List[Person]] [1] and I want to get the List[Person] from it. How can I do it ? import scala.concurrent.Future val futPersons : Future[List[Person]] = .... 回答1: There are multiple ways: futPersons.map { personList => .... } This map returns another Future composed with whatever you return from the map. The map will execute only if the future completes successfully. If you need to handle failure you can use onComplete futPersons.onComplete { case Success(personList) => ... case

Why are Futures within Futures running sequentially when started on Akka Dispatcher

╄→гoц情女王★ 提交于 2019-12-19 02:49:33
问题 We observed a strange behavior when we tried to start a number of futures from within an actor's receive method. If we use our configured dispatchers as ExecutionContext, the futures run on the same thread and sequentially. If we use ExecutionContext.Implicits.global, the futures run in parallel as expected. We boiled down the code to the following example (a more complete example is below): implicit val ec = context.getDispatcher Future{ doWork() } // <-- all running parallel Future{ doWork(