如何与 Rails 4 同时处理请求? [英] How can I serve requests concurrently with Rails 4?

查看:13
本文介绍了如何与 Rails 4 同时处理请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Rails 4 中同时处理多个请求,我可以使用 Rails 3 中的 config.threadsafe!Puma 轻松完成这项工作.

I'm trying to serve multiple requests concurrently in Rails 4, something I was able to do very easily with config.threadsafe! and Puma in Rails 3.

说我有这个控制器

class ConcurrentController < ApplicationController
  def index
    sleep 10000
  end

  def show
  end
end

我曾经可以使用 puma -t 2:16 -p 3000(最少 2 个线程)启动 puma,然后点击 index 然后show 并且仍然有 show 正确呈现.

I used to be able to just start puma with puma -t 2:16 -p 3000 (for min 2 threads) and hit index and then show and still have show render properly.

在 Rails 4 中,如果我尝试做同样的事情,Puma 现在会锁定 index 请求并且 show 永远不会被渲染.当我为服务器点击 Ctrl-C 时,Puma 给了我这个错误:

In Rails 4, if I attempt to do the same thing Puma now locks on the index request and show never gets rendered. When I hit Ctrl-C for the server Puma gives me this error:

Rack app error: #<ThreadError: Attempt to unlock a mutex which is locked by another thread>

要让并发性与 Rails 4 一起工作,我在这里缺少什么?config.threadsafe! 应该不需要(即使我尝试也没有区别)

What am I missing here to get concurrency to work with Rails 4? config.threadsafe! is supposed to not be needed (and doesn't make a difference even if I try)

推荐答案

我邀请你阅读这篇文章中config.threadsafe!的配置选项正在删除 config.threadsafe!它将帮助您更好地理解 config.threadsafe! 的选项,特别是允许并发.

I invite you to read about the configuration options of config.threadsafe! in this article Removing config.threadsafe! It will help you to understand better the options of config.threadsafe!, in particular to allow concurrency.

在 Rails 4 中 config.threadsafe! 是默认设置的.

In Rails 4 config.threadsafe! is set by default.

在 Rails 4 中,默认情况下,DEV 环境中的 Rack::Lock 中间件将请求包裹在互斥体周围.

In Rails 4 requests are wrapped around a Mutex by the Rack::Lock middleware in DEV environments by default.

如果您想启用并发,您可以设置config.allow_concurrency=true.这将禁用 Rack::Lock 中间件.我不会像在您的问题的另一个答案中提到的那样删除它;对我来说,这看起来像是一个黑客.

If you want to enable concurrency you can set config.allow_concurrency=true. This will disable the Rack::Lock middleware. I would not delete it as mentioned in another answer to your question; that looks like a hack to me.

注意:如果您有 config.cache_classes=true 则对 config.allow_concurrency(Rack::Lock 请求互斥锁)的分配获胜不拿效果,默认允许并发请求.如果你有config.cache_classes=false,然后就可以设置config.allow_concurrencytruefalse.在开发中你想要这样的环境

Note: If you have config.cache_classes=true then an assignment to config.allow_concurrency (Rack::Lock request mutex) won't take effect, concurrent requests are allowed by default. If you have config.cache_classes=false, then you can set config.allow_concurrency to either true or false. In DEV environment you would want to have it like this

config.cache_classes=false
config.allow_concurrency=true

声明:这意味着如果 config.cache_classes = false(在 dev env 中默认是这样)我们不能并发请求. 不正确.

附录

您可以参考这个答案,其中设置了一个使用 MRI 和 JRuby 测试并发性的实验.结果令人惊讶.MRI 比 JRuby 快.

Appendix

You can refer to this answer, which sets up an experiment testing concurrency using MRI and JRuby. The results are surprising. MRI was faster than JRuby.

MRI 并发实验是 在 GitHub 上.实验只测试并发请求.控制器中没有竞争条件.但是,我认为实现上面文章中的示例来测试控制器中的竞争条件并不太难.

The experiment with MRI concurrency is on GitHub. The experiment only tests concurrent request. There are no race conditions in the controller. However, I think it is not too difficult to implement example from the article above to test race conditions in a controller.

这篇关于如何与 Rails 4 同时处理请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆