`
cxh116
  • 浏览: 74903 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

经测试,RUBY的线程用在网络这一块还是有点用

阅读更多
当网络阻塞时,虽然RUBY的线程是非原生线程,其作用还是在

服务器端
用phpg写了一个小页面,软件是用nginx+fastcgi,spawn-fcgi设置-C 为 20,启动20个进程,这样在多线程测试才有效,不然就是一个php cgi进程上面的队列,必须等第一个请求完成,第二个请求才会处理

php页面代码
<?php
  sleep(3);
  phpinfo();
?>


rub测试端
require 'open-uri'
s = Time.now
puts s
10.times do
  open("http://localhost/index.php")
end
e = Time.now
puts e
puts "no thread #{e -s}"

threads = []
s = Time.now
puts s
10.times do
  threads.push(Thread.new{ open("http://localhost/index.php") })
end
while true
  threads.each {|t| 
    if t.alive?
      Thread.pass
      break
    else
      threads.delete t
    end
  }
  break if threads.length < 1
end
e = Time.now
puts e
puts "thread #{e -s}"


测试结果,不用线程耗时30秒,启动10个线程,耗时3秒
分享到:
评论
2 楼 cxh116 2011-10-31  
skandhas 写道
Ruby1.9的线程早就是原生线程了。

1.9虽然是原生线程,但还是有 全局解释器锁 ,应该没有多大差别
1 楼 skandhas 2011-10-31  
Ruby1.9的线程早就是原生线程了。

相关推荐

Global site tag (gtag.js) - Google Analytics