翻译 Ruby 并发模型介绍 (一二部分)

xyuchen · September 12, 2017 · Last by xyuchen replied at September 13, 2017 · 6252 hits

翻译了两篇关于 Ruby 并发模型的文章,首次发帖,欢迎 feedback。

第一篇:https://chenxiyu.github.io/2017/09/06/Introduction_to_Concurrency_Models_with_Ruby_Part_I/

第二篇:https://chenxiyu.github.io/2017/09/09/Introduction_to_Concurrency_Models_with_Ruby_Part_II/ 原文地址在译文前后

看了这两篇一头撞进 elixir,erlang 的 actor 模型+ruby 的语法。

Reply to flowerwrong

那有没有一手的经验可以分享呢?

很好,帮助大家从 ruby 的角度理解并发模型。

erlang, clojure 天生对并发真的很友好,比如 clojure

(defn foo1
  []
  (println "f1 start")
  (Thread/sleep 500)
  (println "f1 end")

  "f1")

(defn foo2
  []
  (println "f2 start")
  (Thread/sleep 1000)
  (println "f2 end")
  "f2")

;; 并行
(time
  (let [f2 (future (foo2))
        f1 (future (foo1))]
    (str @f1 @f2)))
;; f2 start
;; f1 start
;; f1 end
;; f2 end
;; "Elapsed time: 1006.12004 msecs"
;; "f1f2"

;; 串行
(time
  (let [f2 (foo2)
        f1 (foo1)]
    (str f1 f2)))
;; f2 start
;; f2 end
;; f1 start
;; f1 end
;; "Elapsed time: 1505.08541 msecs"
;; "f1f2"

但这并不能掩盖,没有 debugger,对程序员不友好的缺点。

Reply to chenge

谢谢鼓励

You need to Sign in before reply, if you don't have an account, please Sign up first.