Popen3 Problem

Thread Solved
Reply

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 Quick reply to this message  
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

Re: Popen3 Problem

 
0
  #2
Dec 14th, 2008
I figured it out!

It wasn't that the program wasn't exiting. Ruby didn't know how to handle a nil variable in my code.

I changed my start_read function to:

  1. def start_read( stdout , stderr )
  2. puts("Read thread is running ...")
  3. loop do
  4. # errorline = stderr.readline
  5. #puts("STDOUT #{errorline}")
  6. the_input = stdout.getc
  7. if the_input == nil # <=== This is what I added.
  8. Thread.kill(@write_thread) #
  9. break #
  10. end #
  11. nodeSendData( @node_number, the_input.chr )
  12. STDOUT.flush
  13. end
  14. end

So, when the program exits the input becomes nil. After discovering that it is nil, I killed the other thread. Everything works great now.

Thanks for nobody's help

- Matt
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC