title: Ruby Sinatra with consumer thread and job queue
body: <p>I’m trying to create a very simple restful server. When it receives a request, I want to create a new job on a queue that can be handled by another thread while the current thread returns a response to the client.</p>
  
  <p>I looked at Sinatra, but haven't got too far.</p>
  
  <pre><code>require 'sinatra'
  require 'thread'
  
  queue = Queue.new
  
  set :port, 9090
  
  get '/' do
    queue &lt;&lt; 'item'
    length = queue.size
    puts 'QUEUE LENGTH %d', length
    'Message Received'
  end
  
  consumer = Thread.new do
    5.times do |i|
      value = queue.pop(true) rescue nil
      puts "consumed #{value}"
    end
  end
  
  consumer.join
  </code></pre>
  
  <p>In the above example, I know the consumer thread would only run a few times (as opposed to the life of the application), but even this isn't working for me. </p>
  
  <p>Is there a better approach?</p>