RSS Forums RSS

Popen3 Problem

Please support our Perl advertiser: Programming Forums
Thread Solved
Reply
Posts: 27
Reputation: calcop is an unknown quantity at this point 
Solved Threads: 0
calcop calcop is offline Offline
Light Poster

Popen3 Problem

  #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. )

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
AddThis Social Bookmark Button
Reply With Quote  
Posts: 27
Reputation: calcop is an unknown quantity at this point 
Solved Threads: 0
calcop calcop is offline Offline
Light Poster

Re: Popen3 Problem

  #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:

def start_read( stdout , stderr )
  puts("Read thread is running ...")
  loop do
    # errorline = stderr.readline
    #puts("STDOUT #{errorline}")
    the_input = stdout.getc
    if the_input == nil                        # <=== This is what I added.
      Thread.kill(@write_thread)          #
      break                                          #
    end                                               #
    nodeSendData( @node_number, the_input.chr )
    STDOUT.flush
  end
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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the Perl Forum
Views: 553 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:15 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC