Non-blocking readline for STDIN?

I cannot understand why IO methods will not work in STDIN if they are correctly set to non-blocking mode:

  require 'fcntl'

  stdin_flags = STDIN.fcntl(Fcntl::F_GETFL)
  p stdin_flags   #32770
  p STDIN.fcntl(Fcntl::F_SETFL, stdin_flags | Fcntl::O_NONBLOCK) # 0
  p STDIN.fcntl(Fcntl::F_GETFL)    # 34818
  #at_exit { STDIN.fcntl(Fcntl::F_SETFL, stdin_flags & ~Fcntl::O_NONBLOCK) }
  STDIN.readline    # this call blocks, IO::EAGAINWaitReadable expected
  exit

IO.fcntlsuccessfully establishes a non-blocking mode, but all input-output functions, such as read, readline, gets, readcharignore mode and hangs when reading, when the input is received.

Setting the synchronization mode does not affect true.

If replaced STDIN.readlinewith a shell call system('read line'), it will work correctly. It will not wait or wait for input, depending on whether non-blocking mode has been set.

I know IO.read_nonblock, but I'm looking for an efficient way to read lines with a trailing newline. The call read_nonblockfor each individual character is very slow.

- () ?

+4
1

, IO , .

IO.select , , , , , . Timeout, - .

, / , IO.read_nonblock .

+2

All Articles