future

Is there a way to cancel/detach a future in C++11?

感情迁移 提交于 2019-11-27 03:43:05
I have the following code: #include <iostream> #include <future> #include <chrono> #include <thread> using namespace std; int sleep_10s() { this_thread::sleep_for(chrono::seconds(10)); cout << "Sleeping Done\n"; return 3; } int main() { auto result=async(launch::async, sleep_10s); auto status=result.wait_for(chrono::seconds(1)); if (status==future_status::ready) cout << "Success" << result.get() << "\n"; else cout << "Timeout\n"; } This is supposed to wait 1 second, print "Timeout", and exit. Instead of exiting, it waits an additional 9 seconds, prints "Sleeping Done", and then segfaults. Is

Scala's “for comprehension” with futures

半世苍凉 提交于 2019-11-27 02:42:38
I am reading through the Scala Cookbook ( http://shop.oreilly.com/product/0636920026914.do ) There is an example related to Future use that involves for comprehension. So far my understanding about for comprehension is when use with a collection it will produce another collection with the same type. For example, if each futureX is of type Future[Int] , the following should also be of type Future[Int] : for { r1 <- future1 r2 <- future2 r3 <- future3 } yield (r1+r2+r3) Could someone explain me what exactly happening when use <- in this code? I know if it was a generator it will fetch each

Scala: List[Future] to Future[List] disregarding failed futures

ぐ巨炮叔叔 提交于 2019-11-27 02:37:29
I'm looking for a way to convert an arbitrary length list of Futures to a Future of List. I'm using Playframework, so ultimately, what I really want is a Future[Result] , but to make things simpler, let's just say Future[List[Int]] The normal way to do this would be to use Future.sequence(...) but there's a twist... The list I'm given usually has around 10-20 futures in it, and it's not uncommon for one of those futures to fail (they are making external web service requests). Instead of having to retry all of them in the event that one of them fails, I'd like to be able to get at the ones that

Dartlang wait more than one future

雨燕双飞 提交于 2019-11-27 02:35:21
问题 I want to do something after a lot of future function done,bu I do not know how to write the code in dart? the code like this: for (var d in data) { d.loadData().then() } // when all loaded // do something here but I don't want to wait them one by one: for (var d in data) { await d.loadData(); // NOT NEED THIS } how to write those code in dart? 回答1: You can use Future.wait to wait for a list of futures: import 'dart:async'; Future main() async { var data = []; var futures = <Future>[]; for

Why does Future::select choose the future with a longer sleep period first?

て烟熏妆下的殇ゞ 提交于 2019-11-27 02:22:06
I'm trying to understand Future::select : in this example, the future with a longer time delay is returned first. When I read this article with its example, I get cognitive dissonance. The author writes: The select function runs two (or more in case of select_all ) futures and returns the first one coming to completion. This is useful for implementing timeouts. It seems I don't understand the sense of select . extern crate futures; extern crate tokio_core; use std::thread; use std::time::Duration; use futures::{Async, Future}; use tokio_core::reactor::Core; struct Timeout { time: u32, } impl

AngularJS promises not firing when returned from a service [duplicate]

故事扮演 提交于 2019-11-27 01:42:02
问题 Possible Duplicate: angularjs - promise never resolved in controller I'm wrapping a slow WebSockets server in a AngularJS service, and then calling into that service from my controllers. If I chain callbacks onto callbacks onto callbacks, everything works just fine, any the UI updates asynchronously. When I try to use $q.defer() to clean up that mess of callbacks, it seems my deferred never gets called. I'm familiar with the concepts of deferred from Python's Twisted, so I think conceptually

Pass multiple parameters to concurrent.futures.Executor.map?

荒凉一梦 提交于 2019-11-27 01:14:20
问题 The concurrent.futures.Executor.map takes a variable number of iterables from which the function given is called. How should I call it if I have a generator that produces tuples that are normally unpacked in place? The following doesn't work because each of the generated tuples is given as a different argument to map: args = ((a, b) for (a, b) in c) for result in executor.map(f, *args): pass Without the generator, the desired arguments to map might look like this: executor.map( f, (i[0] for i

How to carry on executing Future sequence despite failure?

眉间皱痕 提交于 2019-11-27 01:08:21
问题 The traverse method from Future object stops at first failure. I want a tolerant/forgiving version of this method which on occurrence of errors carries on with the rest of the sequence. Currently we have added the following method to our utils: def traverseFilteringErrors[A, B <: AnyRef] (seq: Seq[A]) (f: A => Future[B]): Future[Seq[B]] = { val sentinelValue = null.asInstanceOf[B] val allResults = Future.traverse(seq) { x => f(x) recover { case _ => sentinelValue } } val successfulResults =

Python: Wait on all of `concurrent.futures.ThreadPoolExecutor`'s futures

那年仲夏 提交于 2019-11-27 00:52:21
问题 I've given concurrent.futures.ThreadPoolExecutor a bunch of tasks, and I want to wait until they're all completed before proceeding with the flow. How can I do that, without having to save all the futures and call wait on them? (I want an action on the executor.) 回答1: Just call Executor.shutdown: shutdown(wait=True) Signal the executor that it should free any resources that it is using when the currently pending futures are done executing . Calls to Executor.submit() and Executor.map() made

Scala waiting for sequence of futures

若如初见. 提交于 2019-11-27 00:22:31
问题 I was hoping code like follows would wait for both futures, but it does not. object Fiddle { val f1 = Future { throw new Throwable("baaa") // emulating a future that bumped into an exception } val f2 = Future { Thread.sleep(3000L) // emulating a future that takes a bit longer to complete 2 } val lf = List(f1, f2) // in the general case, this would be a dynamically sized list val seq = Future.sequence(lf) seq.onComplete { _ => lf.foreach(f => println(f.isCompleted)) } } val a = FuturesSequence