title: Thin EventMachine Sinatra vs. Rails
body: <p>I have been looking into the possibility of backgrounding some jobs with EventMachine.  In Sinatra this appears to work great but Rails 3 appears to execute all ticks before rendering a view.</p>
  
  <p>When I run the following code under the thin webserver it behaves as expected.  The first request returns immediately and the second request is waiting for the 3 second sleep call to finish. This is the expected behavior.</p>
  
  <pre><code>class EMSinatra &lt; Sinatra::Base
    get "/" do
      EM.next_tick { sleep 3 }
      "Hello"
    end
  end
  </code></pre>
  
  <p>Whereas in Rails 3 running I am trying to do the same thing: (running under thin)</p>
  
  <pre><code>class EmController &lt; ApplicationController
    def index
      EM.next_tick {
        sleep(3)
      }
    end
  end
  </code></pre>
  
  <p>In Rails the sleep call happens before rendering the view to the browser.  The result is that I am waiting for 3 seconds for the initial page to render.  </p>
  
  <p>Does anybody know why this is happening?  I am not looking for comments on wether this is a good practice or not.  I am simply experimenting.  Throwing small tasks into the reactor loop seems like an interesting thing to look into.  Why should the client have to wait if I am going to make some non-blocking http-requests?</p>