What is the point of choosing a kernel in Ruby?

I am working on a ruby ​​script that ultimately runs a system process that takes quite a while. I need to read this process from stderr and respond to it depending on what is output.

I am currently doing this as:

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 

But I also saw implementations to achieve something similar, but I am doing this with 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 

I read the pickaxe description, which makes a choice, but am confused by why I should (if I should) use it? The first method works the same way.

+4
source share
1 answer

There are probably two reasons:

  • You can use a timeout that you cannot use with each_line
  • You can wait for more than one IO object, for example. d. io = select([stdout, stderr]) and more than one event (for example, a write event or an exception)
+6
source

Source: https://habr.com/ru/post/1402659/


All Articles