My Rails应用程序使用inherited_resources gem.我目前正在努力加快速度,以便能够处理更大的数据集.所以我继续前进(在Bullet宝石的帮助下)使用渴望加载,这将证明是有用的.在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
Backtrace在底部.
我已经尝试过在这个问题的最佳答案中讨论的解决方案:How can I serve requests concurrently with 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 `unlock' /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/lock.rb:22:in `ensure in call' /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 `service' /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service' /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run' /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': undefined method `shutdown' for nil:NilClass (NoMethodError) from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:280:in `block in start' from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `call' from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `join' from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `block (2 levels) in start' from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `each' from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `block in start' from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:32:in `start' from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:162:in `start' from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/handler/webrick.rb:34:in `run' from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:286:in `start' from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/server.rb:80:in `start' from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:80:in `block in server' from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `tap' from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `server' from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!' from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands.rb:17:in `' from bin/rails:4:in `require' from bin/rails:4:in `'
编辑:我开始怀疑这是不是一个线程错误,而不是我的数据集太大而不能进行预先加载,是否有一个阈值,你渴望加载这么多的对象,当它在终端中快速加载时,浏览器只是永远坐在那里?
最佳答案 从WEBrick切换到
Puma是一个选项吗?我有一个需要并发请求的应用程序.我收到你描述的错误:
错误ThreadError:尝试解锁由另一个线程锁定的互斥锁
切换到Puma解决了这个问题的开箱即用.