future

Does a Future timeout kill the Thread execution

删除回忆录丶 提交于 2019-11-29 21:14:48
When using an ExecutorService and Future objects (when submitting Runnable tasks), if I specify a timeout value to the future's get function, does the underlying thread get killed when a TimeoutException is thrown? It does not. Why would it? Unless you tell it to. There is a very valid concern here in case of a Callable for example. If you waited for the result for say 20 seconds and you did not get it, then you are not interested in the result anymore. At that time you should cancel the task at all. Something like this: Future<?> future = service.submit(new MyCallable()); try { future.get(100

ProcessPoolExecutor from concurrent.futures way slower than multiprocessing.Pool

十年热恋 提交于 2019-11-29 20:26:19
I was experimenting with the new shiny concurrent.futures module introduced in Python 3.2, and I've noticed that, almost with identical code, using the Pool from concurrent.futures is way slower than using multiprocessing.Pool . This is the version using multiprocessing: def hard_work(n): # Real hard work here pass if __name__ == '__main__': from multiprocessing import Pool, cpu_count try: workers = cpu_count() except NotImplementedError: workers = 1 pool = Pool(processes=workers) result = pool.map(hard_work, range(100, 1000000)) And this is using concurrent.futures: def hard_work(n): # Real

When to use promise over async or packaged_task?

ぃ、小莉子 提交于 2019-11-29 19:26:53
When should I use std::promise over std::async or std::packaged_task ? Can you give me practical examples of when to use each one of them? user2622016 std::async std::async is a neat and easy way to get a std::future , but: It does not always it start a new thread; pass std::launch::async as a first parameter to force it. auto f = std::async( std::launch::async, func ); The std::~future destructor can block until the new thread finishes auto sleep = [](int s) { std::this_thread::sleep_for(std::chrono::seconds(s)); }; { auto f = std::async( std::launch::async, sleep, 5 ); } Normally we expect

How can I use a QFutureWatcher with QtConcurrent::run() without a race condition

拥有回忆 提交于 2019-11-29 18:48:01
问题 If I understand the following code from the QFutureWatcher documentation correctly, then there is a race condition between the last to lines: // Instantiate the objects and connect to the finished signal. MyClass myObject; QFutureWatcher<int> watcher; connect(&watcher, SIGNAL(finished()), &myObject, SLOT(handleFinished())); // Start the computation. QFuture<int> future = QtConcurrent::run(...); watcher.setFuture(future); If the function ... in the QtConcurrent::run(...) finishes before the

Why is this not compiling? (RValue as thread CTOR arguments)

本小妞迷上赌 提交于 2019-11-29 17:13:44
Hello here is a test code I wrote on MSVC12. Could someone tell me why the std::move when I pass parameters to the thread are not converting the variabes to RValue refs?? And what I should do. Thank you! ///some arbitrary long task std::string DumpFile(std::string path){ std::this_thread::sleep_for(std::chrono::seconds(10)); return path; } void run_promise(std::promise<std::string> &&_prom, std::string &&_path){ try { std::string val = DumpFile(std::move(_path)); _prom.set_value(val); } catch (...) { _prom.set_exception(std::current_exception()); } } std::future<std::string> ADumpFile(std:

How do I wait for a Scala future's onSuccess callback to complete?

旧时模样 提交于 2019-11-29 17:09:53
问题 In Scala, I can use Await to wait for a future to complete. However, if I have registered a callback to run upon completion of that future, how can I wait not only for the future to complete but also for that callback to finish? Here is a minimal but complete program to illustrate the problem: import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.concurrent.{ Await, Future } object Main { def main(args: Array[String]): Unit = { val f:

How to return a future combinator with `&self`

点点圈 提交于 2019-11-29 14:43:27
I have this piece of code: impl ArcService for (Box<MiddleWare<Request>>, Box<ArcService>) { fn call(&self, req: Request, res: Response) -> Box<Future<Item = Response, Error = Error>> { box self.0.call(req).and_then(move |req| self.1.call(req, res)) } } pub trait ArcService: Send + Sync { fn call(&self, req: Request, res: Response) -> Box<Future<Item = Response, Error = Error>>; } pub trait MiddleWare<T>: Sync + Send { fn call<'a>(&'a self, param: T) -> Box<Future<Item = T, Error = Error> + 'a>; } type MiddleWareFuture<'a, I> = Box<Future<Item = I, Error = Error> + 'a>; impl MiddleWare<Request

Futures / Success race

折月煮酒 提交于 2019-11-29 14:41:13
I'm learning futures, and I'm trying to create a method that, take two futures as parameter ( f and g ) and return the first future that was successfully completed, otherwise it returns f or g . Some use cases to illustrate the behaviour of my method are : Future 1 | Future 2 | Result Success First Success Second Future 1 Success First Failure Second Future 1 Success Second Success First Future 2 Success Second Failure First Future 1 Failure First Failure Second Future 2 (because we had a failure on Future 1, so try to see what is the result Future 2) So I created this method : def successRace

converting Akka's Future[A] to Future[Either[Exception,A]]

a 夏天 提交于 2019-11-29 14:40:55
问题 Is there a method in Akka (or in the standard library in Scala 2.10) to convert a Future[A] which might fail into a Future[Either[Exception,A]] ? I know that you can write f.map(Right(_)).recover { case e:Exception => Left(e) } It just seems to be such a common task that I wonder whether I have overlooked something. I'm interested in answers for Scala 2.9/Akka and Scala 2.10. 回答1: The primary reason why this method is missing is that it does not really have good semantics: the static type

Unknown exception from std::promise

穿精又带淫゛_ 提交于 2019-11-29 14:00:17
What is wrong with the following code? When ran the program aborts with an unknown exception #include <iostream> #include <future> int main() { auto promise = std::promise<int>{}; auto future_one = promise.get_future(); promise.set_value(1); return 0; } The error output is terminate called after throwing an instance of 'std::system_error' what(): Unknown error -1 Aborted (core dumped) g++ --version for me gives g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty;