Rails 如何在 Rails development 模式下开启多线程并行处理

ibachue · January 20, 2013 · Last by KoALa replied at February 04, 2013 · 7680 hits

Hi all, 以前一直觉得如果开发时选择的 Rails 服务器非默认的话(比如 thin)就可以实现并行处理(显然被那句 Maximum connections set to 1024 迷惑了),但是今天做了下试验发现似乎不是这样的 依然是单线程串行执行。由于项目中有些测试涉及到并行 因此希望把服务器启动在 development 模式下可以并行的状态。我尝试过例如于

rails s --threaded
script/server thin start --threadedfor Rails 2.3
bundle exec thin start --threaded

等几种方法,均失败。 特此请教诸位 谢谢

我是这样试的

我在 rails3 里,development.rb 里写config.threadsafe! 然后bundle exec thin start --thread 得到 thin 的 pid,top -pid thin_pid 终端里 repeat 10 (curl localhost:3000) 看到#TH 从 2 变成了 22,应该是开启了 (Mac 下的 top)

P.S. rails s不行

使用 thin 开启线程模式,貌似不太稳定,我上次测试的时候,会有一些请求直接返回 500 错误.. thin 自己也说是 experimental 阶段。你可以改用 Puma 在 rails 项目下使用 puma 代替 rails s 即可,因为支持 rack, 默认其会启用 16 个线程。(记得开启 Rails 自己的 config.threadsafe! )

#2 楼 @wppurking 额 好像还是不行 在开发模式下 puma 同样没有看出并行特性,即使已经用puma -t 8:32至少 8 线程了

#4 楼 @iBachue 是不是 development.rb 文件中没有设置 config.threadsafe! 呢?你还可以参考一下这篇文章

测试环境用 unicorn 就可以啦

7 Floor has deleted
You need to Sign in before reply, if you don't have an account, please Sign up first.