Note that there are some explanatory texts on larger screens.

plurals
  1. POAsynchronously iterating over the response of a request using Thin and Sinatra
    primarykey
    data
    text
    <p>If your response in Sinatra returns an 'eachable' object, Sinatra's event loop will 'each' your result and yield the results in a streaming fashion as the HTTP response. However, if there are concurrent requests to Sinatra, it will iterate through all the elements of one response before handling another request. If we have a cursor to the results of some DB query, that means we have to wait for all the data to be available before handling a concurrent query.</p> <p>I've looked at the async-sinatra gem and <a href="http://macournoyer.com/blog/2009/06/04/pusher-and-async-with-thin/" rel="nofollow">http://macournoyer.com/blog/2009/06/04/pusher-and-async-with-thin/</a>, thinking these would solve my problem, but I've tried out this example:</p> <pre><code>require 'sinatra/async' class AsyncTest &lt; Sinatra::Base register Sinatra::Async aget '/' do body "hello async" end aget '/delay/:n' do |n| EM.add_timer(n.to_i) { body { "delayed for #{n} seconds" } } end end </code></pre> <p>and the <code>/delay/5</code> request doesn't work concurrently as I expect it to, i.e. I make 3 requests concurrently and Chrome's debugger notes the response times as roughly 5, 10, and 15 seconds.</p> <p>Am I missing some setup or is there another way to tell Sinatra/Thin to handle requests in a concurrent manner?</p> <p>Update: Here's another wrench in this (or possibly clears things up): Running <code>curl -i <a href="http://localhost:3000/delay/5" rel="nofollow">http://localhost:3000/delay/5</a></code> concurrently has the correct behavior (2 requests each come back in ~5 seconds). Running <code>ab -c 10 -n 50 <a href="http://locahost:3000/delay/5" rel="nofollow">http://locahost:3000/delay/5</a></code> (the Apache benchmark utility) also returns something reasonable for the total time (~25 seconds). Firefox exhibits the same behavior as Chrome. What are the browsers doing different from the command-line utilities?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload