【发布时间】:2012-02-15 15:30:27
【问题描述】:
我在使用 popen3 时遇到了意外行为,我想用它来运行类似 tool ala cmd < file1 > file2 这样的命令。下面的示例挂起,因此永远无法到达stdout done。使用cat 以外的其他工具可能会导致挂起,因此永远无法到达stdin done。我怀疑,我正在遭受缓冲,但我该如何解决这个问题?
#!/usr/bin/env ruby
require 'open3'
Open3.popen3("cat") do |stdin, stdout, stderr, wait_thr|
stdin.puts "foobar"
puts "stdin done"
stdout.each_line { |line| puts line }
puts "stdout done"
puts wait_thr.value
end
puts "all done"
【问题讨论】:
-
我添加了
popen3标签。