我正在研究一个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的描述,选择了什么,但我很困惑为什么我应该(如果我应该)使用它?第一种方法的作用相同.
解决方法:
可能有两个原因:
>您可以使用超时,而不能使用each_line
>您可以等待多个IO对象,例如: G. io = select([stdout,stderr])和多个事件(例如写事件或异常)