John Dibling
John Dibling

Reputation: 101456

IO.popen(my_cmd) - when is the subprocess done?

pipe = IO.popen("my_cmd 2>&0")
while ???
  line = pipe.gets
  puts line if some_condition
end

This is using Ruby 1.8.7 on Windows. my_cmd is an application that prints database records to the stdout. One line per database record, and there's no way to know how many records there will be before I run the command. Each gets call returns one record. Each gets call could take a while, too. It's going over a network to a very large database, so the gets call won't be instant.

I'm processing the input just fine. The problem is, the loop never ends.

my_cmd terminates when it reaches the end of the database. How does my Ruby script know when my_cmd is done? What should ??? be in the code I posted above?

Upvotes: 1

Views: 421

Answers (1)

rogerdpack
rogerdpack

Reputation: 66751

Either

  while !out.eof?

or when you

 Process.wait pipe.pid

http://en.wikibooks.org/wiki/Ruby_Programming/Running_Multiple_Processes

Upvotes: 2

Related Questions