future

Wait until all Future.onComplete callbacks are executed

旧时模样 提交于 2019-11-30 03:42:35
问题 I am using the Future API from Scala 2.10.X. Here is my use case: object Class1 { def apply(f: (Int) => Future[String])(i: Int): Future[String] = { val start = DateTime.now val result = f(i) result.onComplete{ case _ => println("Started at " + start + ", ended at " + DateTime.now) } result } } Pretty simple I think: I'm adding an onComplete callback to my future. Now, I'm wondering if there is a way to add a callback for when the onComplete is done executing - in this example know when the

Kill or timeout a Future in Scala 2.10

点点圈 提交于 2019-11-30 03:19:02
问题 Hi, I'm using Scala 2.10 with the new futures library and I'm trying to write some code to test an infinite loop. I use a scala.concurrent.Future to run the code with the loop in a separate thread. I would then like to wait a little while to do some testing and then kill off the separate thread/future. I have looked at Await.result but that doesn't actually kill the future. Is there any way to timeout or kill the new Scala 2.10 futures? I would prefer not having to add external dependencies

How do you post a boost packaged_task to an io_service in C++03?

大兔子大兔子 提交于 2019-11-30 03:11:27
问题 This is a follow-on from a previous question (here), but I'm working on a multithreaded application and I would like to post a Boost packaged_task to a threaded io_service. I'm stuck using a C++03 compiler (so std::move is out), and the packaged_task is not copyable. I've tried wrapping it in a shared_ptr and passing that, and a lot of other things. Here is my current attempt and the subsequent compiler errors. Any idea how to get this to work? boost::asio::io_service io_service; boost:

Why doesn't Scala's Future have a .get / get(maxDuration) method, forcing us to resort to Await.result() instead?

佐手、 提交于 2019-11-30 03:09:10
问题 Is there any particular advantage in decoupling the get method from the Future class (where I'd expect it to reside) and to instead force the coder to have to know about this external two-method class called Await ? 回答1: Is there any particular advantage in decoupling the get method from the Future class Yes, to make it difficult for the developer to do the wrong thing . A Future represents a computation which will complete in the future and might not be available at the current invocation

How to convert Map[A,Future[B]] to Future[Map[A,B]]?

老子叫甜甜 提交于 2019-11-30 02:55:47
I've been working with the Scala Akka library and have come across a bit of a problem. As the title says, I need to convert Map[A, Future[B]] to Future[Map[A,B]] . I know that one can use Future.sequence for Iterables like Lists, but that doesn't work in this case. I was wondering: is there a clean way in Scala to make this conversion? See if this works for you: val map = Map("a" -> future{1}, "b" -> future{2}, "c" -> future{3}) val fut = Future.sequence(map.map(entry => entry._2.map(i => (entry._1, i)))).map(_.toMap) The idea is to map the map to an Iterable for a Tuple of the key of the map

[高并发Java 七] 并发设计模式

 ̄綄美尐妖づ 提交于 2019-11-30 01:14:59
1. 什么是设计模式 在软件工程中,设计模式(design pattern)是对软件设计中普遍存在(反复出现)的各种问题 ,所提出的解决方案。这个术语是由埃里希·伽玛(Erich Gamma)等人在1990年代从建筑设计领 域引入到计算机科学的。 著名的4人帮: Erich Gamma, Richard Helm, Ralph Johnson ,John Vlissides (Gof) 《设计模式:可复用面向对象软件的基础》收录23种模式 2. 单例模式 单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为 比如:全局信息配置 单例模式最简单的实现: public class Singleton { private Singleton() { System.out.println("Singleton is create"); } private static Singleton instance = new Singleton(); public static Singleton getInstance() { return instance; } } 由私有构造方法和static来确定唯一性。 缺点:何时产生实例 不好控制 虽然我们知道,在类Singleton第一次被加载的时候,就产生了一个实例。

秒杀 tj/co 的 hprose 协程库

妖精的绣舞 提交于 2019-11-30 01:13:07
tj/co 有以下几个方面的问题: 首先, tj/co 库中的 yield 只支持 thunk 函数,生成器函数,promise 对象,以及数组和对象,但是不支持普通的基本类型的数据,比如 null , 数字,字符串等都不支持。这对于 yield 一个类型不确定的变量来说,是很不方便的。而且这跟 await 也是不兼容的。 其次,在 yield 数组和对象时, tj/co 库会自动对数组中的元素和对象中的字段递归的遍历,将其中的所有的 Promise 元素和字段替换为实际值,这对于简单的数据来说,会方便一些。但是对于带有循环引用的数组和对象来说,会导致无法获取到结果,这是一个致命的问题。即使对于不带有循环引用结构的数组和对象来说,如果该数组和对象比较复杂,这也会消耗大量的时间。而且这跟 await 也是不兼容的。 再次,对于 thunk 函数, tj/co 库会认为回调函数第一个参数必须是表示错误,从第二个参数开始才表示返回值。而这对于回调函数只有一个返回值参数的函数,或者回调函数的第一个参数不表示错误的函数来说, tj/co 库就无法使用了。 而 hprose.co 对 yield 的支持则跟 await 完全兼容,支持对所有类型的数据进行 yield 。 当 hprose.co 对 chunk 函数进行 yield 时,如果回调函数第一个参数是 Error

Scala实战:并发-Future和Promise

徘徊边缘 提交于 2019-11-30 01:12:57
并发编程是很困难的,特别是在你没有很好的设计与抽像你的功能层次时。传统的并发解决方案是采用多线程和共享变量,这使得随着代码的增加你很难找到错误根源。 Scala中采用了更好的方案,它不是只基于更低层次的线程的。Scala为用户提供了更高级的抽象: Futures 和 Promises ( Akka 还提供了基于 actor 模式的编程范式,是一种更高层次的并发编程抽象。本文主要讲解 Futures 和 Promises ,这里提供一些进一步学习的参考)。 Future Future 是持有某个值的对象,它完成一些计算并允许在“将来”的某一个时刻获取结果。它也可以是其它计算结果的结果(简单点说就是多个 Future 可以嵌套)。创建一个 Future 最简单的方式就调用它的 apply 方法,它将直接开始一个异步计算,并返回一个 Future 对象,它将包含计算结果。 import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.util.{Success, Failure} def computation(): Int = { 25 + 50 } val theFuture = Future { computation() } 第一行导入了

How do I wait for asynchronous tasks to complete in scala?

て烟熏妆下的殇ゞ 提交于 2019-11-29 23:10:51
I'm aware that my problem might seem a little bit complex. But I'll try to express myself well. I have this method which I want to return a Map[String, List[String]] filled with data. def myFunction():Map[String, List[String]] = { val userMap = Map[String, String](("123456", "ASDBYYBAYGS456789"), ("54321", "HGFDSA5432")) //the result map to return when all data is collected and added val resultMap:Future[Map[String, List[String]]] //when this map is finished (filled) this map is set to resultMap val progressMap = Map[String, List[String]]() for(user <- userMap){ //facebook graph API call to

Futures - map vs flatmap

不羁的心 提交于 2019-11-29 22:21:30
I've read the docs about map and flatMap and I understand that flatMap is used for an operation that accepts a Future parameter and returns another Future . What I don't fully understand is why I would want to do this. Take this example: User hits my webservice asking to "do stuff" I download a file (which is slow) I process the file (which is CPU intensive) Render the result I understand that I would want to use a future to download the file but I have have two options re processing it: val downloadFuture = Future { downloadFile } val processFuture = downloadFuture map { processFile }