EventMachine and looping

Viewed 2207

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)
    }
}

The callbacks fire, that is, I get the string outputs of the status, etc.

But what I want it to do is fire the callbacks, then repeat. There is more logic I plan to implement, such as tweaking the URL each time, but for now, I just want it to:

  1. Retrieve the URL
  2. Fire the callbacks
  3. Repeat...

My understanding about this pattern was that everything in that loop fires, then returns, then goes on forever until I do an EM.stop.

Right now, it retrieves the URL data, and just seems to hang.

Do I need to do a return of some sort to continue here? Why is it hanging, and not looping over and over?

If I surround the entire above code block with a loop do ... end it works as expected.. is that the correct way to implement this? I suppose I am confused as I thought everything within EM.run repeats when it completes.

1 Answers
Related