do |a, b| a / b end end context 'without asking for specific value' do it 'should not raise exceptions' do expect { thread_pool.perform; thread_pool.wait }.not_to raise_error end end context 'with asking for specific value' do it 'should raise an exception' do expect { thread_pool.perform; thread_pool.values[0] }.to raise_error(ZeroDivisionError) end end end
pid = fork do result = result + 5 Marshal.dump result, write exit 0 end write.close result = read.read Process.wait pid puts Marshal.load result # => 10
:balance def initialize(balance) @balance = balance end def withdraw(amount) if @balance >= amount @balance -= amount end end end DRb.start_service( 'druby://192.168.0.1:8787', Account.new(500) ) DRb.thread.join
serves one request at a time • Each worker uses one database connection • Each worker has part of a Rails stack loaded into memory • 0 requests can be served on a free Heroku account
own thread • Each thread has only own context loaded in memory • Each thread requires an own database connection • Can server multiple requests at the same time on a free Heroku account
• Processing by HighlightsController#index • Started GET "/" for 212.142.101.97 • Started GET "/" for 212.142.101.97 • Processing by HighlightsController#index • Processing by HighlightsController#index • Completed 200 OK in 79ms • Completed 200 OK in 67ms • Completed 200 OK in 94ms
threaded • Rails stack in each worker • Will run 0 jobs on a free Heroku account • Single process • Multiple threaded • Rails stack in the main thread only • Will run multiple jobs on a free Heroku account
1 web dyno for “test-app” • Set 1 worker dyno for “test-app-worker” • Configure both to use the same database (e.g. MONGOHQ_URL) and Redis (e.g. REDISTOGO_URL) • More at: https://github.com/phoet/freemium
do |on| on.message do |event, data| data = JSON.parse(data) highlight = Highlight.find data['id'] response.stream.write( "data: #{{id: highlight._id}.to_json}\n\n" ) end end rescue IOError logger.info 'Stream closed' ensure redis.quit response.stream.close end
all my models • Multiple Redis connections • Use app server connection pool • To many connection may bring down the whole app • "data: #{{id: highlight._id}.to_json }\n\n"
|ws| ws.onopen do sid = @channel.subscribe do |data| EM.synchrony do data = JSON.parse(data) highlight = Highlight.find(data['id']) data = { id: highlight.id } ws.send data.to_json end end ws.onclose do @channel.unsubscribe sid end end end end