Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy Sinatra request takes EM thread?
    primarykey
    data
    text
    <p>Sinatra app receives requests for long running tasks and EM.defer them, launching them in EM's internal pool of 20 threads. When there are more than 20 EM.defer running, they are stored in EM's threadqueue by EM.defer.</p> <p>However, it seems Sinatra won't service any requests until there is an EM thread available to handle them. My question is, isn't Sinatra suppose to use the reactor of the main thread to service all requests? Why am I seeing an add on the threadqueue when I make a new request?</p> <p>Steps to reproduce:</p> <pre><code>Access /track/ Launch 30 /sleep/ reqs to fill the threadqueue Access /ping/ and notice the add in the threadqueue as well as the delay </code></pre> <p>Code to reproduce it:</p> <pre><code>require 'sinatra' #monkeypatch EM so we can access threadpools module EventMachine def self.queuedDefers @threadqueue==nil ? 0: @threadqueue.size end def self.availThreads @threadqueue==nil ? 0: @threadqueue.num_waiting end def self.busyThreads @threadqueue==nil ? 0: @threadpool_size - @threadqueue.num_waiting end end get '/track/?' do EM.add_periodic_timer(1) do p "Busy: " + EventMachine.busyThreads.to_s + "/" +EventMachine.threadpool_size.to_s + ", Available: " + EventMachine.availThreads.to_s + "/" +EventMachine.threadpool_size.to_s + ", Queued: " + EventMachine.queuedDefers.to_s end end get '/sleep/?' do EM.defer(Proc.new {sleep 20}, Proc.new {body "DONE"}) end get '/ping/?' do body "pong" end </code></pre> <p>I tried the same thing on Rack/Thin (no Sinatra) and works as it's supposed to, so I guess Sinatra is causing it.</p> <pre><code>Ruby version: 1.9.3.p125 EventMachine: 1.0.0.beta.4.1 Sinatra: 1.3.2 OS: Windows </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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