Ruby 多線程的潛力和弱點分析_ruby專題

來源:互聯網
上載者:User

Web 應用程式大多是 IO 密集型的,利用 Ruby 多進程+多執行緒模式將能大幅提升系統輸送量。其原因在於:當Ruby 某個線程處於 IO Block 狀態時,其它的線程還可以繼續執行。但由於存在 Ruby GIL (Global Interpreter Lock),MRI Ruby 並不能真正利用多線程進行並行計算。JRuby 去除了 GIL,是真正意義的多線程,既能應付 IO Block,也能充分利用多核 CPU 加快整體運算速度。

上面說得比較抽象,下面就用例子一一加以說明。

Ruby 多線程和 IO Block

先看下面一段代碼(示範目的,沒有實際用途):

複製代碼 代碼如下:

# File: block_io1.rb

def func1
  puts "sleep 3 seconds in func1\n"
  sleep(3)
end

def func2
  puts "sleep 2 seconds in func2\n"
  sleep(2)
end

def func3
  puts "sleep 5 seconds in func3\n"
  sleep(5)
end

func1
func2
func3

代碼很簡單,3 個方法,用 sleep 類比耗時的 IO 操作。 運行代碼(環境 MRI Ruby 1.9.3) 結果是:

複製代碼 代碼如下:

$ time ruby block_io1.rb
sleep 3 seconds in func1
sleep 2 seconds in func2
sleep 5 seconds in func3

real  0m11.681s
user  0m3.086s
sys 0m0.152s

比較慢,時間都耗在 sleep 上了,總共花了 10 多秒。

採用多線程的方式,改寫如下:

複製代碼 代碼如下:

# File: block_io2.rb

def func1
  puts "sleep 3 seconds in func1\n"
  sleep(3)
end

def func2
  puts "sleep 2 seconds in func2\n"
  sleep(2)
end

def func3
  puts "sleep 5 seconds in func3\n"
  sleep(5)
end

threads = []
threads << Thread.new { func1 }
threads << Thread.new { func2 }
threads << Thread.new { func3 }

threads.each { |t| t.join }

啟動並執行結果是:

複製代碼 代碼如下:

$ time ruby block_io2.rb
sleep 3 seconds in func1
sleep 2 seconds in func2
sleep 5 seconds in func3

real  0m6.543s
user  0m3.169s
sys 0m0.147s

總共花了 6 秒多,明顯快了許多,只比最長的 sleep 5 秒多了一點。

上面的例子說明,Ruby 的多線程能夠應付 IO Block,當某個線程處於 IO Block 狀態時,其它的線程還可以繼續執行,從而使整體處理時間大幅縮短。


Ruby GIL 的影響

還是先看一段代碼(示範目的):

複製代碼 代碼如下:

# File: gil1.rb

require 'securerandom'
require 'zlib'

data = SecureRandom.hex(4096000)

16.times { Zlib::Deflate.deflate(data) }

代碼先隨機產生一些資料,然後對其進行壓縮,壓縮是非常耗 CPU 的,在我機器(雙核 CPU, MRI Ruby 1.9.3)運行結果如下:

複製代碼 代碼如下:

$ time ruby gil1.rb

real  0m8.572s
user  0m8.359s
sys 0m0.102s

更改為多線程版本,代碼如下:

複製代碼 代碼如下:

# File: gil2.rb

require 'securerandom'
require 'zlib'

data = SecureRandom.hex(4096000)

threads = []
16.times do
  threads << Thread.new { Zlib::Deflate.deflate(data) }
end

threads.each {|t| t.join}

多線程的版本運行結果如下:

複製代碼 代碼如下:

$ time ruby gil2.rb

real  0m8.616s
user  0m8.377s
sys 0m0.211s

從結果可以看出,由於 MRI Ruby GIL 的存在,Ruby 多線程並不能重複利用多核 CPU,使用多線程後整體所花時間並不縮短,反而由於線程切換的影響,所花時間還略有增加。

JRuby 去除了 GIL

使用 JRuby (我的機器上是 JRuby 1.7.0)運行 gil1.rb 和 gil2.rb,得到很不一樣的結果。

複製代碼 代碼如下:

$ time jruby gil1.rb

real  0m12.225s
user  0m14.060s
sys 0m0.615s


複製代碼 代碼如下:

$ time jruby gil2.rb

real  0m7.584s
user  0m22.822s
sys 0m0.819s


可以看到,JRuby 使用多線程時,整體已耗用時間有明顯縮短(7.58 比 12.22),這是由於 JRuby 去除了 GIL,可以真正並行的執行多線程,充分利用了多核 CPU。

總結:Ruby 多線程可以在某個線程 IO Block 時,依然能夠執行其它線程,從而降低 IO Block 對整體的影響,但由於 MRI Ruby GIL 的存在,MRI Ruby 並不是真正的並存執行,JRuby 去除了 GIL,可以做到真正的多線程並存執行。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.