943,793 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Marked Solved
  • Views: 916
  • Perl RSS
Dec 14th, 2008
0

Popen3 Problem

Expand Post »
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. )

Perl Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
calcop is offline Offline
27 posts
since Oct 2005
Dec 14th, 2008
0

Re: Popen3 Problem

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:

Perl Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
calcop is offline Offline
27 posts
since Oct 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: How To Save in our Computer
Next Thread in Perl Forum Timeline: How to restrict WMI ports are random(Use Perl)?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC