LoginSignup
5
7

More than 5 years have passed since last update.

IO#pipeは書き込み続けると止まる

Posted at

IO#pipeは読み出さずに書き込み続けるとブロックされる。

r, w = IO.pipe
20000.times { |n|
    print "\b\b\b\b\b%05d" % n
    w.print "hoge"  # 16384回ぐらいで止まる
}
w.close

スレッドの外側で読み出し続ければ止まらない。

r, w = IO.pipe
Thread.fork do
    20000.times { |n|
        print "\b\b\b\b\b%05d" % n
        w.print "hoge"
    }
    w.close
end
r.read  # 読み出せば止まらない

Thread#joinを使うとなぜか止まる。

r, w = IO.pipe
Thread.fork do
    20000.times { |n|
        print "\b\b\b\b\b%05d" % n
        w.print "hoge"  # 16384回ぐらいで止まる
    }
    w.close
end.join
r.read

Thread#abort_on_exceptionなら止まらない。

r, w = IO.pipe
Thread.fork do
    20000.times { |n|
        print "\b\b\b\b\b%05d" % n
        w.print "hoge"  # 止まらない
    }
    w.close
end.abort_on_exception = true
r.read
5
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7