简体   繁体   English

Twitter未来优于Scala Future的优势是什么?

[英]What are advantages of a Twitter Future over a Scala Future?

I know a lot of reasons for Scala Future to be better. 我知道Scala Future更好的原因很多。 Are there any reasons to use Twitter Future instead? 是否有任何理由使用Twitter Future? Except the fact Finagle uses it. 除了Finagle使用它之外。

Disclaimer: I worked at Twitter on the Future implementation. 免责声明:我在Twitter上就Future的实施工作。 A little bit of context, we started our own implementation before Scala had a "good" implementation of Future . 在一点上下文中,我们在Scala实现Future的“良好”实现之前就开始了我们自己的实现。

Here're the features of Twitter's Future : 以下是Twitter Future的特点:

  • Some method names are different and Twitter's Future has some new helper methods in the companion. 一些方法名称不同,Twitter的Future在同伴中有一些新的帮助方法。

eg Just one example: Future.join(f1, f2) can work on heterogeneous Future types. 例如,一个例子: Future.join(f1, f2)可以处理异构的Future类型。

Future.join(
  Future.value(new Object), Future.value(1)
).map {
  case (o: Object, i: Int) => println(o, i)
}

o and i keep their types, they're not casted into the least common supertype Any . oi保持他们的类型,他们没有被铸造成最不常见的超类型Any

  • A chain of onSuccess is guaranteed to be executed in order: eg: 保证onSuccess链按顺序执行:例如:

     f.onSuccess { println(1) // #1 } onSuccess { println(2) // #2 } 

#1 is guaranteed to be executed before #2 保证#1在#2之前执行

  • The Threading model is a little bit different. 线程模型有点不同。 There's no notion of ExecutionContext, the Thread that set the value in a Promise (Mutable implementation of a Future) is the one executing all the computations in the future graph. 没有ExecutionContext的概念,在Promise(Future的可变实现)中设置值的Thread是在将来的图中执行所有计算的。 eg: 例如:

     val f1 = new Promise[Int] f1.map(_ * 2).map(_ + 1) f1.setValue(2) // <- this thread also executes *2 and +1 
  • There's a notion of interruption/cancellation. 有一个中断/取消的概念。 With Scala's Futures, the information only flows in one direction, with Twitter's Future, you can notify a producer of some information (not necessarily a cancellation). 使用Scala的Futures,信息只向一个方向流动,通过Twitter的Future,您可以向生产者通知一些信息(不一定是取消)。 In practice, it's used in Finagle to propagate the cancellation of a RPC. 在实践中,它在Finagle中用于传播RPC的取消。 Because Finagle also propagates the cancellation across the network and because Twitter has a huge fan out of requests, this actually saves lots of work. 因为Finagle还通过网络宣传取消,并且由于Twitter有大量粉丝请求,这实际上节省了大量工作。

     class MyMessage extends Exception val p = new Promise[Int] p.setInterruptHandler { case ex: MyMessage => println("Receive MyMessage") } val f = p.map(_ + 1).map(_ * 2) f.raise(new MyMessage) // print "Receive MyMessage" 
  • Until recently, Twitter's Future were the only one to implement efficient tail recursion (ie you can have a recursive function that call itself without blowing up you call stack). 直到最近,Twitter的Future才是唯一一个实现高效尾递归的人(即你可以拥有一个递归函数来调用自身而不会炸掉你的调用堆栈)。 It has been implemented in Scala 2.11+ (I believe). 它已在Scala 2.11+中实现(我相信)。

As far as I can tell the main difference that could go in favor of using Twitter's Future is that it can be cancelled, unlike scala's Future . 据我所知,可以支持使用Twitter的Future的主要区别在于它可以取消,与scala的Future不同。

Also, there used to be some support for tracing the call chains (as you probably know plain stack traces are close to being useless when using Futures). 此外,曾经有一些支持跟踪调用链(因为你可能知道使用Futures时普通堆栈跟踪几乎没用)。 In other words, you could take a Future and tell what chain of map / flatMap produced it. 换句话说,你可以使用Future并告诉map / flatMap产生了什么链。 But the idea has been abandoned if I understand correctly. 但如果我理解正确的话,这个想法就被抛弃了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM