1

I use EventMachine.open_keyboard in my irc client app that uses rbcurse (ncurses gem), as follows:

Fiber.new do
    EM.open_keyboard(NbKeyboard) do |kb|
        $input = Readline.readline("")
        $buffer << $input
    end
end.resume

This code has to be in a Fiber and catch input in a nonblocking way. After I try to type a second line, ruby throws a "dead fiber" exception. I tried adding loops, but that blocks as well, and changing the fiber into a thread doesn't work with the keyboard module. Other ways I tried block all my threads, one of which is responsible for keeping the buffer updated/refreshed. How can I change the code to keep the Fiber open/alive after typing the first line? To view the keyboard module (didn't write it myself), go here.

Edit: Thanks to the answer of Sawa I figured it out:

Fiber.new do
    EM.open_keyboard(NbKeyboard) do |kb|
        loop do
            $input = Readline.readline("")
            $buffer << $input
            Fiber.yield
        end
    end
 end.resume

1 Answers1

0

Add

loop{Fiber.yield}

at the end of the fiber body.

sawa
  • 165,429
  • 45
  • 277
  • 381
  • Thank you for your answer, and it somewhat helped, the application doesn't crash and I can type as many lines as I want, but they are not added to the buffer. How to fix this? – user3261959 Feb 02 '14 at 06:44