title: EventMachine with em-synchrony I need to correctly throttle my http requests
body: <p>I have a consumer which pulls messages off of a queue via an evented subscription. It takes those messages and then connects with a rather slow http interface.  I have a worker pool of 8 and once those are all filled up I need to stop pulling requests from the queue and have the fibers that are working on the http jobs keep working.  Here is an example I've thrown together.</p>
  
  <pre><code>def send_request(callback)
    EM.synchrony do
  
      while $available &lt;= 0
  
        sleep 2 
  
        puts "sleeping"
      end 
      url = 'http://example.com/api/Restaurant/11111/images/?image%5Bremote_url%5D=https%3A%2F%2Firs2.4sqi.net%2Fimg%2Fgeneral%2Foriginal%2F8NMM4yhwsLfxF-wgW0GA8IJRJO8pY4qbmCXuOPEsUTU.jpg&amp;image%5Bsource_type_enum%5D=3'
      result = EM::Synchrony.sync EventMachine::HttpRequest.new(url, :inactivity_timeout =&gt; 0).send("apost", :head =&gt; {:Accept =&gt; 'services.v1'})
  
      callback.call(result.response) 
    end 
  end
  
  def display(value)
    $available += 1
    puts value.inspect
  end
  
  $available = 8 
  
  EM.run do
    EM.add_periodic_timer(0.001) do
      $available -= 1
      puts "Available: #{$available}"
  
      puts "Tick ..." 
      puts send_request(method(:display))
    end 
  
  end
  </code></pre>
  
  <p>I have found that if I call sleep within a while loop in the synchrony block, the reactor loop gets stuck.  If I call sleep within an if statement(sleeping just once) then most times it is enough time for the requests to finish but it is unreliable at best.  If I use EM::Synchrony.sleep, then the main reactor loop will keep creating new requests.</p>
  
  <p>Is there a way to pause the main loop but have the fibers finish their execution?</p>