经测试,RUBY的线程用在网络这一块还是有点用
当网络阻塞时,虽然RUBY的线程是非原生线程,其作用还是在
服务器端
用phpg写了一个小页面,软件是用nginx+fastcgi,spawn-fcgi设置-C 为 20,启动20个进程,这样在多线程测试才有效,不然就是一个php cgi进程上面的队列,必须等第一个请求完成,第二个请求才会处理
php页面代码
<?php sleep(3); phpinfo();?>
rub测试端
require 'open-uri's = Time.nowputs s10.times do open("http://localhost/index.php")ende = Time.nowputs eputs "no thread #{e -s}"threads = []s = Time.nowputs s10.times do threads.push(Thread.new{ open("http://localhost/index.php") })endwhile true threads.each {|t| if t.alive? Thread.pass break else threads.delete t end } break if threads.length < 1ende = Time.nowputs eputs "thread #{e -s}"
测试结果,不用线程耗时30秒,启动10个线程,耗时3秒 1 楼 skandhas 2011-10-31 Ruby1.9的线程早就是原生线程了。 2 楼 cxh116 2011-10-31 skandhas 写道Ruby1.9的线程早就是原生线程了。
1.9虽然是原生线程,但还是有 全局解释器锁 ,应该没有多大差别