How to wait for process to finish using IO.popen?

后端 未结 6 1579
北海茫月
北海茫月 2020-12-29 06:14

I\'m using IO.popen in Ruby to run a series of command line commands in a loop. I then need to run another command outside of the loop. The command outside of t

相关标签:
6条回答
  • 2020-12-29 06:22

    I suggest you use Thread.join to synchronize the last popen call:

    t = Thread.new do
        for foo in bar
           IO.popen(cmd_foo)
        end
    end
    
    t.join
    
    IO.popen(another_cmd)
    
    0 讨论(0)
  • 2020-12-29 06:28

    Do you need the output of popen? If not, do you want to use Kernel#system or some other command?

    0 讨论(0)
  • 2020-12-29 06:32

    Apparently the canonical way to do this is:

     Process.wait(popened_io.pid)
    
    0 讨论(0)
  • 2020-12-29 06:33

    I think you'd need to assign the results from the IO.popen calls within the cycle to the variables, and keep calling read() on them until eof() becomes true on all.

    Then you know that all the programs have finished their execution and you can start another_cmd.

    0 讨论(0)
  • 2020-12-29 06:34
    for foo in bar
      out = IO.popen(cmd_foo)
      out.readlines
    end
    IO.popen(another_cmd)
    

    Reading the output to a variable then calling out.readlines did it. I think that out.readlines must wait for the process to end before it returns.

    Credit to Andrew Y for pointing me in the right direction.

    0 讨论(0)
  • 2020-12-29 06:37

    Use the block form and read all the content:

    IO.popen "cmd" do |io|
      # 1 array
      io.readlines
    
      # alternative, 1 big String
      io.read
    
      # or, if you have to do something with the output
      io.each do |line|
        puts line
      end
    
      # if you just want to ignore the output, I'd do
      io.each {||}
    end
    

    If you do not read the output, it may be that the process blocks because the pipe connecting the other process and your process is full and nobody reads from it.

    0 讨论(0)
提交回复
热议问题