4

我的 Rails 应用程序使用inherited_resourcesgem。我目前正在尝试加快速度,以便能够处理更大的数据集。所以我继续前进(在 Bullet gem 的帮助下)在证明有帮助的地方使用急切加载。在inherited_resources 中,它看起来像这样:

def collection
  my_widgets ||= end_of_association_chain.includes(:association_one, :association_two, :association_three, :association_four)

  @widgets = case params[:filter]
                  when nil then my_widgets.all
                  when 'old' then my_widgets.old
                  when 'new' then my_widgets.new
                  end

  @final_collection
end

新代码是.includes(:association_one, :association_two, :association_three, :association_four)

对于我的测试大型数据库,这个简单的更改将我在日志中的加载时间加快了大约 5 倍。但是,浏览器只会坐在那里,一片空白。没有什么。随着 chrome 加载图标的运行。

当我在终端中杀死服务器时,我得到了这个相当独特的错误:

 ERROR ThreadError: Attempt to unlock a mutex which is locked by another thread

回溯在底部。

我已经尝试过在这个问题的最佳答案中讨论的解决方案:如何使用 Rails 4 同时提供请求?,具有以下内容:

#config/environments/development.rb
config.cache_classes = false
config.eager_load = true
config.allow_concurrency=true

我在这里想念什么?为什么简单地将这些关联添加到预先加载中,这显着加快了幕后的事情,借给我一个无响应的浏览器与这个 ThreadError?

    /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/lock.rb:22:in `解锁'
    /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/lock.rb:22:in `确保通话中'
    /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/lock.rb:23:in `call'
    /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/content_length.rb:15:in `call'
    /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `服务'
    /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:在“服务”中
    /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:在“运行”中
    /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
/Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/handler/webrick.rb:48:in `shutdown': nil:NilClass 的未定义方法`shutdown' (无方法错误)
    来自 /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:280:in `block in start'
    来自 /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `call'
    来自 /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `join'
    来自 /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `block (2 levels) in start'
    来自 /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `each'
    来自 /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `block in start'
    来自 /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:32:in `start'
    来自 /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:162:in `start'
    来自 /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/handler/webrick.rb:34:in `run'
    来自 /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:286:in `start'
    来自 /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/server.rb:80:in `start'
    来自 /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    来自 /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `tap'
    来自 /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `server'
    来自 /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    来自 /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands.rb:17:in `'
    从 bin/rails:4:in `require'
    从 bin/rails:4:in `'

编辑:我开始怀疑这是否比我的数据集太大而无法急切加载的线程错误更少,是否存在一个阈值,您急切加载如此多的对象,虽然它在终端中快速加载,但浏览器只是永远坐在那里?

4

1 回答 1

1

从 WEBrick 切换到Puma是一种选择吗?我有一个需要并发请求的应用程序。我收到了您描述的错误:

ERROR ThreadError: Attempt to unlock a mutex which is locked by another thread

切换到 Puma 开箱即用地解决了这个问题。

于 2016-06-22T16:15:22.627 回答