Twitter Future 与 Scala Future 相比有哪些优势? [英] What are advantages of a Twitter Future over a Scala Future?

查看:61
本文介绍了Twitter Future 与 Scala Future 相比有哪些优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Scala Future 变得更好的很多原因.是否有任何理由改用 Twitter Future?除了事实 Finagle 使用它.

I know a lot of reasons for Scala Future to be better. Are there any reasons to use Twitter Future instead? Except the fact Finagle uses it.

推荐答案

免责声明:我在 Twitter 工作,负责 Future 的实现.一点上下文,我们在 Scala 有一个好的"Future 实现之前就开始了我们自己的实现.

Disclaimer: I worked at Twitter on the Future implementation. A little bit of context, we started our own implementation before Scala had a "good" implementation of Future.

以下是 Twitter 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.

例如举个例子:Future.join(f1, f2) 可以处理异构的 Future 类型.

e.g. Just one example: Future.join(f1, f2) can work on heterogeneous Future types.

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

oi 保持它们的类型,它们不会被转换为最不常见的超类型 Any.

o and i keep their types, they're not casted into the least common supertype Any.

  • 一个 onSuccess 链保证按顺序执行:例如:

  • A chain of onSuccess is guaranteed to be executed in order: e.g.:

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

#1 保证在#2 之前执行

#1 is guaranteed to be executed before #2

  • 线程模型有点不同.没有 ExecutionContext 的概念,在 Promise(未来的可变实现)中设置值的线程是执行未来图中所有计算的线程.例如:

  • 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. e.g.:

val f1 = new Promise[Int]
f1.map(_ * 2).map(_ + 1)
f1.setValue(2) // <- this thread also executes *2 and +1

  • 有中断/取消的概念.使用 Scala 的 Futures,信息只向一个方向流动,使用 Twitter 的 Futures,你可以通知生产者一些信息(不一定是取消).实际上,它在 Finagle 中用于传播 RPC 的取消.由于 Finagle 还会在整个网络中传播取消操作,而且 Twitter 拥有大量的请求粉丝,因此这实际上节省了大量工作.

  • 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). In practice, it's used in Finagle to propagate the cancellation of a 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.

    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"
    

  • 直到最近,Twitter 的 Future 是唯一一个实现高效尾递归的方法(即,您可以使用递归函数调用自身而不会炸毁您的调用堆栈).它已在 Scala 2.11+ 中实现(我相信).

  • Until recently, Twitter's Future were the only one to implement efficient tail recursion (i.e. you can have a recursive function that call itself without blowing up you call stack). It has been implemented in Scala 2.11+ (I believe).

    这篇关于Twitter Future 与 Scala Future 相比有哪些优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆