How do I listen to STDIN input without pausing my script?

后端 未结 2 2056
天涯浪人
天涯浪人 2021-01-06 15:54

I have a while loop consistently listening to incoming connections and outputting them to console. I would like to be able to issue commands via the console wit

2条回答
  •  半阙折子戏
    2021-01-06 16:30

    Ruby uses green threads, so blocking system calls will block all threads anyway. An idea:

    require 'io/wait'
    
    while true
      if $stdin.ready?
        line = $stdin.readline.strip
        p "line from stdin: #{line}"
      end
      p "really, I am working here"
      sleep 0.1
    end
    

提交回复
热议问题