Forum: Ruby Open3

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5282ac6f1068a317a96e51747db52206?d=identicon&s=25 Matt Do (matt_d)
on 2008-12-14 10:37
Hello everyone. I have created a sales system for my company. Basically
you telnet to the system and enter your login name, password ... From
there you can access the sales system.

I am working on a feature to write reports for a certain area of the
system. For this, I want it to just shell out to VIM (Vi iMproved). For
this I will be using Popen3, so the telnet user can access the sytem.

This is some example code I have ...

def runVimForReport( node_number, file_name )
  stdin, stdout, stderr = Open3.popen3('vim #{file_name}' )

  read_thread = Thread.new { start_read(stdout,stderr) }
  write_thread = Thread.new { start_write(stdin,stderr) }

  write_thread.join
  read_thread.join
end

def start_read( stdout , stderr )
  loop do
    the_input = stdout.getc
    nodeSendData( @node_number, the_input.chr )
    STDOUT.flush
  end

  puts("EXIT READ THREAD")
end

def start_write( stdin , stderr )
  loop do
    STDOUT.flush
    input = nodeGetChar( @node_number )
    stdin.putc(input)
  end
end

This code works well, the user can access VIM just fine. There is only
one problem ...

When the user exits the program it continues to read/write to the
process. It doesn't just simply exit the functions. How do I tell when a
program has exited? I tried other programs and all are the same ... It
just hangs. The user has to logout and log back in. Even when
disconnecting the telnet session, the program is still running in the
background.

Is there a variable I can check in my loop to see if the program has
exited?

Thanks for all your help!

- Matt
5282ac6f1068a317a96e51747db52206?d=identicon&s=25 Matt Do (matt_d)
on 2008-12-14 11:35
Got it fixed. It wasn't that the program wasn't exiting it was that the
ruby program. I added this to my start_read thread.

    if the_input == nil
      Thread.kill(@write_thread)
      break
    end
This topic is locked and can not be replied to.