Thread: Popen3 Problem
View Single Post
Join Date: Oct 2005
Posts: 27
Reputation: calcop is an unknown quantity at this point 
Solved Threads: 0
calcop calcop is offline Offline
Light Poster

Popen3 Problem

 
0
  #1
Dec 14th, 2008
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 ... ( I know its Ruby code, but I know that Popen3 is popular with Perl. I'm sure a Perl user can answer this question as it more than likely isn't a problem with the language itself. )

  1. def runVimForReport( node_number, file_name )
  2. stdin, stdout, stderr = Open3.popen3('vim #{file_name}' )
  3.  
  4. read_thread = Thread.new { start_read(stdout,stderr) }
  5. write_thread = Thread.new { start_write(stdin,stderr) }
  6.  
  7. write_thread.join
  8. read_thread.join
  9. end
  10.  
  11. def start_read( stdout , stderr )
  12. loop do
  13. the_input = stdout.getc
  14. nodeSendData( @node_number, the_input.chr )
  15. STDOUT.flush
  16. end
  17.  
  18. puts("EXIT READ THREAD")
  19. end
  20.  
  21. def start_write( stdin , stderr )
  22. loop do
  23. STDOUT.flush
  24. input = nodeGetChar( @node_number )
  25. stdin.putc(input)
  26. end
  27. 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
Reply With Quote