Ruby on Rails does not do multithreaded request-responses very well, or at least, ActiveRecord doesn't.
Ruby on Rails不能很好地执行多线程请求响应,或者至少ActiveRecord没有。
The notion of only one request-response active at the same time can be a hassle when creating web applications which fork off a shell-command that takes long to finish.
在创建Web应用程序时,只有一个请求响应同时处于活动状态的概念可能会很麻烦,这些应用程序会分离一个需要很长时间才能完成的shell命令。
What I'd like are some of your views on these kinds of setups? Is Rails maybe not a good fit for some applications?
我想要的是你对这些设置的一些看法? Rails可能不适合某些应用程序吗?
Also, what are the current state of affairs in regard to concurrency in Ruby on Rails? What are the best practices. Are there workarounds to the shortcomings?
另外,Ruby on Rails中关于并发性的当前事态是什么?什么是最佳实践。有缺点的解决方法吗?
5 个解决方案
#1
4
Rails currently doesn't handle concurrent requests within a single MRI (Matz Ruby Interpreter) Ruby process. Each request is essentally wrapped with a giant mutex. A lot of work has gone into making the forthcoming Rails 2.2 thread-safe, but you're not going to get a lot of benefit from this when running under Ruby 1.8x. I can't comment on whether Ruby 1.9 will be different because I'm not very familiar with it, but probably not I'd have thought.
Rails目前不能在单个MRI(Matz Ruby Interpreter)Ruby流程中处理并发请求。每个请求都由一个巨大的互斥锁包含。很多工作已经开始使即将推出的Rails 2.2线程安全,但是当你在Ruby 1.8x下运行时,你不会从中获得很多好处。我不能评论Ruby 1.9是否会有所不同,因为我对它不是很熟悉,但可能不是我想的。
One area that does look very promising in this regard is running Rails using JRuby, because the JVM is generally acknowledged as being good at multi-threading. Arun Gupta from Sun Microsystems gave some interesting performance figures on this setup at RailsConf Europe recently.
在这方面看起来非常有前途的一个领域是使用JRuby运行Rails,因为JVM通常被认为擅长多线程。来自Sun Microsystems的Arun Gupta最近在RailsConf Europe上为这个设置提供了一些有趣的性能数据。
#2
3
Neverblock allows for non blocking functionality without modifying the way you write programs. It really is an exciting looking project, and was backported to work on Ruby 1.8.x (it relies on Ruby 1.9's fibers). It works with both PostgreSQL and MySQL to perform non-blocking queries. The benchmarks are crazy...
Neverblock允许非阻塞功能,而无需修改编写程序的方式。它确实是一个令人兴奋的项目,并且已经向后移植到Ruby 1.8.x(它依赖于Ruby 1.9的光纤)。它适用于PostgreSQL和MySQL,以执行非阻塞查询。基准很疯狂......
#3
3
Matz's Ruby 1.8 uses green threads, and Matz's Ruby 1.9 will use native O/S threads. Other implementations of Ruby 1.8, such as JRuby and IronRuby, use native O/S threads. YARV, short for Yet Another Ruby VM, also uses native O/S threads but has a global interpreter lock to ensure that only one Ruby thread is executing at any given time.
Matz的Ruby 1.8使用绿色线程,而Matz的Ruby 1.9将使用本机O / S线程。 Ruby 1.8的其他实现,例如JRuby和IronRuby,使用本机O / S线程。 YARV是Yet Another Ruby VM的缩写,也使用本机O / S线程,但具有全局解释器锁,以确保在任何给定时间只执行一个Ruby线程。
#4
2
If what you run at the shell is not necessary for the rendering of the page (e.g. you're only triggering maintenance tasks or something), you should start them as background processes. Check out starling and workling.
如果您在shell上运行的内容对于页面的呈现不是必需的(例如,您只是触发维护任务或其他内容),则应将它们作为后台进程启动。检查椋鸟和工作。
If this doesn't apply to your situation, you'll have to make sure multiple instances of your app servers run. Traditionally people would start multiple instances of Mongrel. But now I'd say the easiest way to have a solid setup is by far using Phusion Passenger. It's an Apache module that lets you easily specify how many instances (min and max) of your app servers you want to have running. Passenger does the rest. And if I recall correctly, it doesn't do stupid round robin for dispatching requests. I think it's by availability.
如果这不适用于您的情况,则必须确保运行应用服务器的多个实例。传统上人们会启动Mongrel的多个实例。但是现在我要说最简单的方法是使用Phusion Passenger进行稳定的设置。它是一个Apache模块,可让您轻松指定要运行的应用服务器的实例数(最小和最大)。乘客完成其余的工作。如果我没记错的话,它不会为调度请求做愚蠢的循环。我认为这取决于可用性。
#5
1
Ruby 1.9 is adding lightweight Fibers:
Ruby 1.9正在添加轻量级光纤:
#1
4
Rails currently doesn't handle concurrent requests within a single MRI (Matz Ruby Interpreter) Ruby process. Each request is essentally wrapped with a giant mutex. A lot of work has gone into making the forthcoming Rails 2.2 thread-safe, but you're not going to get a lot of benefit from this when running under Ruby 1.8x. I can't comment on whether Ruby 1.9 will be different because I'm not very familiar with it, but probably not I'd have thought.
Rails目前不能在单个MRI(Matz Ruby Interpreter)Ruby流程中处理并发请求。每个请求都由一个巨大的互斥锁包含。很多工作已经开始使即将推出的Rails 2.2线程安全,但是当你在Ruby 1.8x下运行时,你不会从中获得很多好处。我不能评论Ruby 1.9是否会有所不同,因为我对它不是很熟悉,但可能不是我想的。
One area that does look very promising in this regard is running Rails using JRuby, because the JVM is generally acknowledged as being good at multi-threading. Arun Gupta from Sun Microsystems gave some interesting performance figures on this setup at RailsConf Europe recently.
在这方面看起来非常有前途的一个领域是使用JRuby运行Rails,因为JVM通常被认为擅长多线程。来自Sun Microsystems的Arun Gupta最近在RailsConf Europe上为这个设置提供了一些有趣的性能数据。
#2
3
Neverblock allows for non blocking functionality without modifying the way you write programs. It really is an exciting looking project, and was backported to work on Ruby 1.8.x (it relies on Ruby 1.9's fibers). It works with both PostgreSQL and MySQL to perform non-blocking queries. The benchmarks are crazy...
Neverblock允许非阻塞功能,而无需修改编写程序的方式。它确实是一个令人兴奋的项目,并且已经向后移植到Ruby 1.8.x(它依赖于Ruby 1.9的光纤)。它适用于PostgreSQL和MySQL,以执行非阻塞查询。基准很疯狂......
#3
3
Matz's Ruby 1.8 uses green threads, and Matz's Ruby 1.9 will use native O/S threads. Other implementations of Ruby 1.8, such as JRuby and IronRuby, use native O/S threads. YARV, short for Yet Another Ruby VM, also uses native O/S threads but has a global interpreter lock to ensure that only one Ruby thread is executing at any given time.
Matz的Ruby 1.8使用绿色线程,而Matz的Ruby 1.9将使用本机O / S线程。 Ruby 1.8的其他实现,例如JRuby和IronRuby,使用本机O / S线程。 YARV是Yet Another Ruby VM的缩写,也使用本机O / S线程,但具有全局解释器锁,以确保在任何给定时间只执行一个Ruby线程。
#4
2
If what you run at the shell is not necessary for the rendering of the page (e.g. you're only triggering maintenance tasks or something), you should start them as background processes. Check out starling and workling.
如果您在shell上运行的内容对于页面的呈现不是必需的(例如,您只是触发维护任务或其他内容),则应将它们作为后台进程启动。检查椋鸟和工作。
If this doesn't apply to your situation, you'll have to make sure multiple instances of your app servers run. Traditionally people would start multiple instances of Mongrel. But now I'd say the easiest way to have a solid setup is by far using Phusion Passenger. It's an Apache module that lets you easily specify how many instances (min and max) of your app servers you want to have running. Passenger does the rest. And if I recall correctly, it doesn't do stupid round robin for dispatching requests. I think it's by availability.
如果这不适用于您的情况,则必须确保运行应用服务器的多个实例。传统上人们会启动Mongrel的多个实例。但是现在我要说最简单的方法是使用Phusion Passenger进行稳定的设置。它是一个Apache模块,可让您轻松指定要运行的应用服务器的实例数(最小和最大)。乘客完成其余的工作。如果我没记错的话,它不会为调度请求做愚蠢的循环。我认为这取决于可用性。
#5
1
Ruby 1.9 is adding lightweight Fibers:
Ruby 1.9正在添加轻量级光纤: