小编BSM*_*BSM的帖子

Ruby并发:非阻塞I/O与线程

我正在玩Ruby(1.9.3-p0)中的并发,并创建了一个非常简单,I/O密集的代理任务.首先,我尝试了非阻塞方法:

require 'rack'
require 'rack/fiber_pool'
require 'em-http'
require 'em-synchrony'
require 'em-synchrony/em-http'

proxy = lambda {|*|
  result = EM::Synchrony.sync EventMachine::HttpRequest.new('http://google.com').get
  [200, {}, [result.response]]
}

use Rack::FiberPool, :size => 1000
run proxy

=begin
$ thin -p 3000 -e production -R rack-synchrony.ru start
>> Thin web server (v1.3.1 codename Triple Espresso)

$ ab -c100 -n100 http://localhost:3000/
Concurrency Level:      100
Time taken for tests:   5.602 seconds
HTML transferred:       21900 bytes
Requests per second:    17.85 [#/sec] (mean)
Time per request:       5602.174 [ms] (mean)
=end
Run Code Online (Sandbox Code Playgroud)

嗯,我以为我一定做错了.对于我们主要等待I/O的任务,平均请求时间为5.6秒?我尝试了另一个: …

ruby multithreading asynchronous eventmachine

13
推荐指数
1
解决办法
2566
查看次数

标签 统计

asynchronous ×1

eventmachine ×1

multithreading ×1

ruby ×1