future

Does a Future timeout kill the Thread execution

蓝咒 提交于 2019-11-30 10:28:19
问题 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? 回答1: 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

When to use promise over async or packaged_task?

橙三吉。 提交于 2019-11-30 10:13:55
问题 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? 回答1: 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

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

纵然是瞬间 提交于 2019-11-30 09:49:17
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. The primary reason why this method is missing is that it does not really have good semantics: the static type Future[Either[Throwable, T]] does not ensure that that future cannot fail, hence the type change does not gain

Scala-way to handle conditions in for-comprehensions?

与世无争的帅哥 提交于 2019-11-30 09:39:00
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(user) _ = if (!authResult.ok) throw new AuthFailedException(userId) // find the good owned by the user,

Convert a Java Future to a Scala Future

怎甘沉沦 提交于 2019-11-30 09:01:49
I have a Java Future object which I would like to convert into a Scala Future . Looking at the j.u.c.Future API, there is nothing much that I could use other than the isDone method. Is this isDone method blocking? Currently this is what I have in my mind: val p = Promise() if (javaFuture.isDone()) p.success(javaFuture.get) Is there a better way to do this? How about just wrapping it (I'm assuming there's an implicit ExecutionContext here): val scalaFuture = Future { javaFuture.get } EDIT: A simple polling strategy could look like this (java.util.Future => F): def pollForResult[T](f: F[T]):

Await a future, receive an either

五迷三道 提交于 2019-11-30 08:10:47
I'd like to await a scala future that may have failed. If I use Await.result the exception will be thrown. Instead, if I have f: Future[String] I would like a method Await.resultOpt(f): Option[String] or Await.resultEither(f): Either[String] . I could get this by using scala.util.control.Exception.catching or I could f map (Right(_)) recover { case t: Throwable => Left(t) } , but there must be a more straightforward way. You could use Await.ready which simply blocks until the Future has either succeeded or failed, then returns a reference back to that Future. From there, you would probably

Is it possible to install a callback after request processing is finished in Spray?

不羁的心 提交于 2019-11-30 07:19:19
问题 I'm trying to serve large temporary files from Spray. I need to delete those files once HTTP request is complete. I could not find a way to do this so far... I'm using code similar to this or this: respondWithMediaType(`text/csv`) { path("somepath" / CsvObjectIdSegment) { id => CsvExporter.export(id) { // loan pattern to provide temp file for this request file => encodeResponse(Gzip) { getFromFile(file) } } } } So essentially it calls getFromFile which completes the route in a Future . The

failure in Scala future's for comprehension

ⅰ亾dé卋堺 提交于 2019-11-30 05:32:22
I have three sequential Futures and use in the for comprehension like this val comF = for { f1 <- future1 f2 <- future2 f3 <- future3 } yield { // something } comF onSuccess { } comF onFailure { // ---------------- Here is the problem -------------------------------- // // How do I know which future failed(throw exception), when the callback comes here ? // Thanks for the help! Different futures using different exceptions can solve it. } Now I have a future list like List[Future[T]], and first I transfer it to Future[List[T]] using this method ( Why does this list-of-futures to future-of-list

Is concurrent.futures a medicine of the GIL?

人盡茶涼 提交于 2019-11-30 05:19:16
I was just searching about this new implementation, and i use python 2.7, i must install this , so if i use it, i'll forget the word GIL on CPython? No, concurrent.futures has almost nothing whatsoever to do with the GIL. Using processes instead of threads is medicine for the GIL. (Of course, like all medicine, it has side effects. But it works.) The futures module just gives you a simpler way to schedule and wait on tasks than using threading or multiprocessing directly. And it has the added advantage that you can swap between a thread pool and a process pool (and maybe even a greenlet loop,

How can I use shared_ptr using PostThreadMessage?

佐手、 提交于 2019-11-30 05:13:24
问题 I would like to upgrade my MFC production code to use the std::shared_ptr smart pointer when calling other windows or threads. Such calls are SendMessage , PostMessage and PostThreadMessage which pass wparam and lparam and which respectively are an unsigned int and long . Currently, I create a class object, new an object, make the call passing a pointer to the object, use the object on the receiving end and then delete it. Since shared_ptr works so well in the rest of my code I wanted to at