igrigorik / em-synchrony

Fiber aware EventMachine clients and convenience classes
http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers
MIT License
1.04k stars 151 forks source link

em-synchrony retuns nil inside of Thin, with aget, without aget. #152

Open envygeeks opened 11 years ago

envygeeks commented 11 years ago
def get
out=nil
  if !EventMachine.reactor_running?
    EventMachine.run do
      Fiber.new do
        out = Response.new(@url.dup.get)
        EventMachine.stop
      end.resume
    end
  else
    Fiber.new do
      out = Response.new(@url.dup.get)
    end.resume
  end
out
end

Running the above code in a Fiber in Pry works perfectly, running it inside of Thin results in an nil value, actually the oddest part is it replaces @url with nil so I had to start duping just so I could figure out what's going on, but I couldn't. Even when I run the examples from: https://github.com/igrigorik/em-http-request/blob/master/examples/fibered-http.rb inside of Thin, I get the same nil result. Am I doing it wrong?

richo commented 11 years ago

Can you replace out with an array and push values into it instead of setting a local variable?

envygeeks commented 11 years ago

@richo what do you mean? I'll have to create a local no matter what even with an array.

richo commented 11 years ago
def get
  out = []
  if !EventMachine.reactor_running?
    EventMachine.run do
      Fiber.new do
        out << Response.new(@url.dup.get)
        EventMachine.stop
      end.resume
    end
  else
    Fiber.new do
      out << Response.new(@url.dup.get)
    end.resume
  end
  out.pop
end

Almost certainly barking up the wrong tree, I seem to recall there being some weird instance_exec magic that may be creating more scope than you're expecting.

This just guarantees that you're mutating the closed over out and definitely not creating a new one. I'm not really expecting it to the work, but it's the only thing that jumped out at me.

envygeeks commented 11 years ago

@richo didn't work :( and I'm stuck in a crappy situation with this too because Unicorn just doesn't work out well for restarting on file change in development and I can't get Thin + EM to do http requests right, and Puma is broken on jRuby w/ SSL >.< sad day.

richo commented 11 years ago

You can cheat with unicorn, use Guard to send USR2 to the master every time there are fs changes with preload_app :false?

That's kinda shitty but it sounds like your best option right now. That said, unicorn and jruby sounds like Doing It Wrong.

envygeeks commented 11 years ago

Trinidad+jRuby in production/staging -- Thin+MRI on development (my development)... puma came about so that we could kinda bring the two together with a server that works on both without me having to build in certain key pieces to account for Thin quirks... so the comment does seem out of place but it's fitting kinda.