Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby中的Kernelselect有什么意义?_Ruby_Linux - Fatal编程技术网

Ruby中的Kernelselect有什么意义?

Ruby中的Kernelselect有什么意义?,ruby,linux,Ruby,Linux,我正在编写一个ruby脚本,它最终会启动一个需要花费相当长时间的系统进程。我需要从这个过程的标准中读取数据,并根据输出内容做出反应 我目前正在这样做: 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 但我也看到过

我正在编写一个ruby脚本,它最终会启动一个需要花费相当长时间的系统进程。我需要从这个过程的标准中读取数据,并根据输出内容做出反应

我目前正在这样做:

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
但我也看到过实现类似功能的实现,但使用的是:

我已经读过鹤嘴锄对select功能的描述,但我不明白为什么我应该使用它?第一种方法的效果完全相同。

可能有两个原因:

您可以使用timeout,但不能对每一行使用timeout 您可以等待多个IO对象,例如。Gio=选择[stdout,stderr]和多个事件,例如写入事件或异常
我想我不明白为什么我需要等待IO对象?为什么在启动进程后它们不能立即使用?IO对象可以立即使用,但是当您尝试读取标准输出时(例如,如果还没有),您的监视进程将被阻止。假设您的系统进程向stderr打印了一些诊断信息,正在等待您的答复,但它是stderr,而不是stdout,因此您将永远在stdout上等待某些信息。select从发生事件的列表中返回第一个可用的IO对象,这样您就可以处理它,并且读取时不会因为有问题而阻塞。
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