【发布时间】:2011-10-30 22:53:03
【问题描述】:
鉴于我希望测试长命令的非阻塞读取,我创建了以下脚本,将其保存为long,使其可以使用chmod 755 执行,并将其放置在我的路径中(另存为@987654324 @ 其中~/bin 在我的路径中)。
我在使用 RVM 默认值编译的带有 ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0] 的 *nix 变体。我不使用 Windows,因此不确定测试脚本是否适合您。
#!/usr/bin/env ruby
3.times do
STDOUT.puts 'message on stdout'
STDERR.puts 'message on stderr'
sleep 1
end
为什么long_err 会生成每条由“long”打印的 STDERR 消息
def long_err( bash_cmd = 'long', maxlen = 4096)
stdin, stdout, stderr = Open3.popen3(bash_cmd)
begin
begin
puts 'err -> ' + stderr.read_nonblock(maxlen)
end while true
rescue IO::WaitReadable
IO.select([stderr])
retry
rescue EOFError
puts 'EOF'
end
end
long_out 在打印所有 STDOUT 消息之前一直处于阻塞状态?
def long_out( bash_cmd = 'long', maxlen = 4096)
stdin, stdout, stderr = Open3.popen3(bash_cmd)
begin
begin
puts 'out -> ' + stdout.read_nonblock(maxlen)
end while true
rescue IO::WaitReadable
IO.select([stdout])
retry
rescue EOFError
puts 'EOF'
end
end
我假设你会在测试任一功能之前require 'open3'。
为什么IO::WaitReadable 对于 STDOUT 和 STDERR 的提出不同?
如果您有使用other ways to start subprocesses 的变通方法,也很感激。
【问题讨论】: