だから私は次のコードを持っています:
リーダー.rb
require 'open4'
def streamer(stdout)
begin
loop do
data = stdout.read_nonblock(8)
print data
end
rescue Errno::EAGAIN
retry
rescue EOFError
puts 'End of file'
end
end
pid, stdin, stdout, stderr = Open4::popen4 "ruby threader.rb"
stdout.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
streamer(stdout)
threader.rb
10.times do
$stdout.puts "test"
sleep 1
end
1 つの ruby スクリプトは、毎秒 stdout に出力する単純なスピナーです。
もう1つはそのスクリプトを実行するためのもので、データが入ってくるとキャプチャしたいので、ストリームがstdoutノンブロッキングから読み取られるようにします。
私はこれを機能させることができないようです。O_NONBLOCK
fctnlフラグを適切に設定していると思いますが、おそらくそうではありません。