自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Akka 使用系列之四: Future

開發(fā) 開發(fā)工具
這篇文章介紹 Akka 的同步機制,以及 Spark 和 Akka 的恩怨情仇。

這篇文章介紹 Akka 的同步機制,以及 Spark 和 Akka 的恩怨情仇。

Akka

1. Akka 中的 Future

Akka 中的 Actor 發(fā)送和接收消息默認都是異步的。為了說明異步性,我們實行下面的數(shù)學老師和歷史老師的 Actor:

  1. class MathTeacherActor extends Actor with ActorLogging { 
  2.     def receive = { 
  3.         case "1+1等于多少?"           => { 
  4.         Thread.sleep(1) 
  5.         sender ! "1+1等于2" 
  6.         } 
  7.     } 
  8. class HistoryTeacherActor extends Actor with ActorLogging { 
  9.     def receive = { 
  10.         case "歷史上規(guī)模***的眾籌行動是什么?" => { 
  11.             Thread.sleep(1) 
  12.             sender ! "歷史上規(guī)模***的眾籌行動是 +1s" 
  13.         } 
  14.     } 

如果我們在詢問歷史老師之后訪問答案(如下面代碼所示),我們發(fā)現(xiàn)并不能獲取正確答案。原因就在于 Akka 是異步非阻塞的。

  1. val res = historyteacher ? "歷史上規(guī)模***的眾籌行動是什么?" 
  2. println(res) 

實質上, historyteacher ? "歷史上規(guī)模***的眾籌行動是什么?" 返回的根本不是答案,而是一個 Future。在Akka中, 一個Future是用來獲取某個并發(fā)操作的結果的數(shù)據(jù)結構。有了 Future,我們可以以同步(阻塞)或異步(非阻塞)的方式訪問結果。下面是簡單地以同步(阻塞)方式訪問結果的示例。

  1. class StudentActor(mathteacher:ActorRef,historyteacher:ActorRef) 
  2.  extends Actor with ActorLogging{ 
  3.   def receive = { 
  4.     case res:String => { 
  5.         val future1 = historyteacher ? "歷史上規(guī)模***的眾籌行動是什么?" 
  6.         val future2 = mathteacher ? "1+1等于多少?" 
  7.         val res1    = Await.result(future1,10 second) 
  8.         val res2    = Await.result(future2,10 second) 
  9.         println(res1) 
  10.         println(res2) 
  11.     } 
  12.  } 

2. Akka 和 Spark

Spark 一開始使用 Akka 作為內部通信部件。在 Spark 1.3 年代,為了解決大塊數(shù)據(jù)(如Shuffle)的傳輸問題,Spark引入了Netty通信框架。到了 Spark 1.6, Spark 可以配置使用 Akka 或者 Netty 了,這意味著 Netty 可以完全替代 Akka 了。再到 Spark 2, Spark 已經(jīng)完全拋棄 Akka 了,全部使用 Netty 了。Sad。

Netty

為什么 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 和 Akka ,但他們必須使用 Spark 依賴的那個版本的 Akka。Spark 主要用了 Akka 的 RPC 和 單線程 event-loop,因此 Spark 沒有必要依賴完全的 Akka。最終,對 Akka 心心念念的 Spark 用 netty 實現(xiàn)下簡易版本的 Akka。真愛啊。

3. 總結

到這里,Akka 使用系列就結束了。這個系列簡單地過了一下 Akka 的基礎知識,介紹其梗概。

【本文為51CTO專欄作者“李立”的原創(chuàng)稿件,轉載請通過51CTO獲取聯(lián)系和授權】

戳這里,看該作者更多好文

責任編輯:趙寧寧 來源: 51CTO專欄
相關推薦

2017-06-01 22:59:45

Akka層次結構Actors

2018-04-24 15:08:40

2023-11-06 08:35:23

VTK可視化開源軟件庫

2011-11-14 10:10:56

虛擬化vmwareVMware View

2022-06-02 11:12:10

CallableFuture

2021-03-16 14:45:39

Zabbix 5.2Grafana監(jiān)控

2023-11-15 08:28:13

PythonVTK

2009-09-22 13:10:22

ibmdwSOA

2009-06-30 14:52:55

APC

2021-07-19 07:31:08

服務調用Dubbo

2009-09-22 09:02:10

職業(yè)規(guī)劃IT職業(yè)發(fā)展

2011-04-29 10:58:11

SimpleFrame

2016-12-29 11:01:03

LinuxLXD 2.0資源控制

2021-11-09 09:57:46

Webpack 前端分包優(yōu)化

2022-08-30 08:43:11

Spring權限控制

2009-07-07 10:08:49

Future Resp

2009-07-08 13:19:25

Future Resp

2023-11-24 16:13:05

C++編程

2021-04-13 09:15:16

C++插件Nodejs

2020-12-29 10:16:24

接口測試flaskmock
點贊
收藏

51CTO技術棧公眾號