EventMachine and loop

Here is my code:

EventMachine.run {

    conn = EM::Protocols::HttpClient2.connect request.host, 80

    req = conn.get(request.query)
    req.callback { |response|
      p(response.status)
      p(response.headers)
      p(response.content)
    }
}

Callbacks i.e. I get status string outputs, etc.

But what I want is to start callbacks and then retry. I plan to implement more logic, for example, setting up a URL every time, but for now I just want to:

  • Get URL
  • Fry callbacks
  • Repeat ...

My understanding of this pattern was that everything in this loop fires and then returns, and then lasts forever until I do EM.stop.

It is currently retrieving the URL data and just seems to hang.

Do I need to make some sort of refund in order to continue here? Why is it hanging and not looping over and over?

, ... end .. ? , , , , EM.run, , .

+5
1

run, , . , , . run while. , .

, - , , , . EventMachine .

- :

def do_stuff(queue, request = nil)
  request ||= queue.pop
  return unless (request)

  conn = EM::Protocols::HttpClient2.connect request.host, 80

  req = conn.get(request.query)
  req.callback { |response|
    p(response.status)
    p(response.headers)
    p(response.content)

    EventMachine.next_tick do
      # This schedules an operation to be performed the next time through
      # the event-loop. Usually this is almost immediate.
      do_stuff(queue)
    end
  }
end   

:

EventMachine.run do
  queue = [ ... ] # List of things to do
  do_stuff(queue)
end

, , , EventMachine.

+4

All Articles