ITeye博客

线程安全的Rails

andyhu1007 · 2010-04-06 21:33 · 编程语言 · Markdown

Thread Safe Rails? 关于线程安全的Rails,请大家慎重采用,具体看这篇文章:http://m.onkey.org/thread-safety-for-your-rails。

 

上一个项目我们怀着战战兢兢的心情开启了多线程,结果是出乎意料得好。开启多线程后没遇到什么困难,可能会遇到的问题有两点:

 

1. 去掉class variable (当然,基本上你不应该使用类变量)

 

2. 另外一件事情,就是在multiple threads下面Rails需要eager load libs。请移步:http://gigix.thoughtworkers.org/2009/7/17/move-to-multi-threaded-rails

 

 

多线程的Rails对于performance有极大提升,请再次移步:http://blog.headius.com/2008/08/qa-what-thread-safe-rails-means.html   (请自行fan qiang)

 

基本上意思是内存使用量的锐减和数据库连接资源的优化:

 

写道

In a typical Mongrel setup, handling 10 concurrent requests means you have to have 10 copies of Rails loaded, 10 copies of your application loaded, 10 in-memory data caches, 10 database connections...everything has to be scaled in lock step for every additional request you want to handle concurrently.

 

写道

Instead of a single database connection for a given Rails instance, there will be a pool of connections, allowing N database connections to be used by the M requests executing concurrently. It also means allowing requests to potentially execute without consuming a connection, so the number of live, active connections usually will be lower than the number of requests you can handle concurrently.

 

References:

 

教程: http://m.onkey.org/2008/10/23/thread-safety-for-your-rails

Slides:  http://www.slideshare.net/Naoto.Takai/jruby-on-rails-and-thread-safety-presentation 
Q & A: http://stackoverflow.com/questions/647642/ruby-rails-thread-safety

 

----EOF----

评论(2)

  1. #2 楼 · andyhu1007 · 2010-04-10 20:31

    robbin writes:

    何况多进程rails都是每进程hold一个持久连接,数据库资源利用很充分,谈不上多线程能够节省,何况多线程调度连接池也有额外的开销和复杂度。

    headius说道:

    It also means allowing requests to potentially execute without consuming a connection

    potentially~~

  2. #1 楼 · robbin · 2010-04-10 14:56

    ruby本地的lib不是线程安全的,你多线程rails读取本地lib如果遇到阻塞调用整个进程会被全部挂起。

    何况多进程rails都是每进程hold一个持久连接,数据库资源利用很充分,谈不上多线程能够节省,何况多线程调度连接池也有额外的开销和复杂度。

    最后,多进程 vs 多线程 从性能,可扩展性和稳定性来说,多线程并没有什么优势。

评论功能维护中,当前仅展示历史评论。