Akka 使用系列之四: Future

栏目: Scala · 发布时间: 6年前

内容简介:Akka 使用系列之四: Future

这篇文章介绍 Akka 的同步机制,以及 Spark 和 Akka 的恩怨情仇。

Akka 使用系列之四: Future

1 Akka 中的 Future

Akka 中的 Actor 发送和接收消息默认都是异步的。为了说明异步性,我们实行下面的数学老师和历史老师的 Actor:

class MathTeacherActor extends Actor with ActorLogging {
  def receive = {
    case "1+1等于多少?"           => {
      Thread.sleep(1)
      sender ! "1+1等于2"
    }
  }
}

class HistoryTeacherActor extends Actor with ActorLogging {
  def receive = {
    case "历史上规模最大的众筹行动是什么?" => {
      Thread.sleep(1)
      sender ! "历史上规模最大的众筹行动是 +1s"
    }
  }
}

如果我们在询问历史老师之后访问答案(如下面代码所示),我们发现并不能获取正确答案。原因就在于 Akka 是异步非阻塞的。

val res = historyteacher ? "历史上规模最大的众筹行动是什么?"
println(res)

实质上, historyteacher ? "历史上规模最大的众筹行动是什么?" 返回的根本不是答案,而是一个 Future。在Akka中, 一个Future是用来获取某个并发操作的结果的数据结构。有了 Future,我们可以以同步(阻塞)或异步(非阻塞)的方式访问结果。下面是简单地以同步(阻塞)方式访问结果的示例。

class StudentActor(mathteacher:ActorRef,historyteacher:ActorRef) 
extends Actor with ActorLogging{

  def receive = {
    case res:String => {
        val future1 = historyteacher ? "历史上规模最大的众筹行动是什么?"
        val future2 = mathteacher ? "1+1等于多少?"

        val res1    = Await.result(future1,10 second)
        val res2    = Await.result(future2,10 second)

        println(res1)
        println(res2)

    }
  }
}

2 Akka 和 Spark

Spark 一开始使用 Akka 作为内部通信部件。在 Spark 1.3 年代,为了解决大块数据(如Shuffle)的传输问题,Spark引入了Netty通信框架。到了 Spark 1.6, Spark 可以配置使用 Akka 或者 Netty 了,这意味着 Netty 可以完全替代 Akka 了。再到 Spark 2, Spark 已经完全抛弃 Akka 了,全部使用 Netty 了。Sad。

Akka 使用系列之四: Future

为什么 Spark 无情地有步骤有预谋地抛弃 Akka 呢?Spark 官方倒是给了一个说法: https://issues.apache.org/jira/browse/SPARK-5293

A lot of Spark user applications are using (or want to use) Akka. Akka as a whole can contribute great architectural simplicity and uniformity. However, because Spark depends on Akka, it is not possible for users to rely on different versions, and we have received many requests in the past asking for help about this specific issue. For example, Spark Streaming might be used as the receiver of Akka messages - but our dependency on Akka requires the upstream Akka actors to also use the identical version of Akka.

Since our usage of Akka is limited (mainly for RPC and single-threaded event loop), we can replace it with alternative RPC implementations and a common event loop in Spark.

大意就是很多 Spark 用户在使用 Spark 之后,就必须使用 Spark 依赖的那个版本的 Akka。Spark 主要用了 Akka 的 RPC 和 单线程 event-loop,因此 Spark 没有必要依赖完全的 Akka。最终 Spark 用 netty 实现下简易版本的 Akka。真爱啊。

3 总结

到这里,Akka 使用系列就结束了。这个系列简单地过了一下 Akka 的基础知识,介绍其梗概。如果需要深入了解,还需要详细阅读官方文档。完整代码已经上传至 Github

最近在 GitHub 上开发了一个 Side Project - RoomAI 。这个 Side Project 目标是提供一些非完整信息游戏环境,让算法人员开发非完整信息游戏 AI, 目前已经支持德州和梭哈。欢迎大家使用和反馈。我后续会基于这个项目写一些文章介绍非完整信息游戏的算法。

Akka 使用系列之四: Future


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beginning ARKit for iPhone and iPad

Beginning ARKit for iPhone and iPad

Wallace Wang / Apress / 2018-11-5 / USD 39.99

Explore how to use ARKit to create iOS apps and learn the basics of augmented reality while diving into ARKit specific topics. This book reveals how augmented reality allows you to view the screen on ......一起来看看 《Beginning ARKit for iPhone and iPad》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具