RailsCasts - Ruby on Rails Screencasts

RailsCasts Pro episodes are now free!

Learn more or hide this

Thread-Safety

#365 Thread-Safety pro

Jul 09, 2012 | 10 minutes | Performance, Production, Rails 4.0
The config.threadsafe! option will likely be enabled by default in Rails 4.0. Here you will learn what this option does, how it affects production, and some tips on thread safety.
Click to Play Video ▶
Tweet
  • Download:
  • source code Project Files in Zip (45.2 KB)
  • mp4 Full Size H.264 Video (20.1 MB)
  • m4v Smaller H.264 Video (12 MB)
  • webm Full Size VP8 Video (14.6 MB)
  • ogv Full Size Theora Video (25.7 MB)
Browse_code Browse Source Code

Resources

config/production.rb
config.threadsafe!
terminal
rake middleware
rake middleware RAILS_ENV=production
rails g controller foo bar
rails s -e production
repeat 4 (curl http://localhost:3000/foo/bar &)
unicorn -E production -p 3000
rails s puma -e production
foo_controller.rb
class FooController < ApplicationController
 FOO = "bar".freeze
 @@counter = 0
 @@mutex = Mutex.new
 
 def bar
 @@mutex.synchronize do
 counter = @@counter
 sleep 1
 counter += 1
 @@counter = counter
 end
 render text: "#{@@counter}\n"
 end
end
database.yml
pool: 15
loading

AltStyle によって変換されたページ (->オリジナル) /