我正在开发一个ruby脚本,最终启动一个需要很长时间的系统进程。我需要从这个过程的stderr中读取并根据输出的内容做出反应。
我目前正在这样做:
Open3.popen3(cmd_to_run) do |stdin, stdout, stderr, waitthread|
stderr.each_line do |line|
# look out for specific lines and react to them accordingly
end
end
但是我也看到了实现类似功能的实现,但是使用kernel#select:
Open3.popen3(cmd_to_run) do |stdin, stdout, stderr, waitthread|
io = select([stderr], nil, nil, 30)
if io.nil?
log("Command timed out during Kernel#select")
return
end
io[0][0].each_line do |line|
# look out for specific lines and react to them accordingly
end
end
我已经阅读了pick的描述,选择了什么,但我很困惑为什么我应该(如果我应该)使用它?第一种方法也是一样的。
答案 0 :(得分:6)
可能有两个原因:
each_line
IO
个对象,例如: G。 io = select([stdout, stderr])
和多个事件(例如写事件或异常)