CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Tk fileevent waits until fork is finished

 



alex_scheibe
Deleted

Apr 25, 2001, 5:17 AM

Post #1 of 2 (909 views)
Tk fileevent waits until fork is finished Can't Post

I have a simple Perl/Tk script which runs an external program and inserts the output of the program into a text widget. This functionality will be part of a bigger program once I get it to work properly. The external program will run for a variable length of time, anywhere from 5 seconds to a couple of minutes, outputting text as it runs, so I want the text widget to be updated while the program is running.

The problem I'm having is that the script waits for the program to be finished running before it inserts the entire output into the text widget.

This is the sample script I'm using to test:


Code
############################################################## 

use Tk;
use strict;

open (FH, "./program |") || die "$!";
my $fh = \*FH;

my $mw = new MainWindow;
my $text = $mw->Scrolled("Text",
-width => 80,
-height => 25,
-scrollbars => "se")
->pack(-expand => 1);


$mw->fileevent($fh, 'readable', [\&InsertText]);


MainLoop;


sub InsertText {
if (my $line = <$fh>) {
$text->insert('end', $line);
$text->yview('end');
$text->update;
} else {
$mw->fileevent($fh, 'readable', '');
close($fh);
}
}

##############################################################

The program.c I use for testing it is this:


Code
////////////////////////////////////////////////////////////// 
#include <stdio.h>
main()
{
printf("This program was created to print out text.\n\n");
sleep(1);
printf("Here is some text I found on Yahoo news:\n\n");
sleep(1);
// etc.. (10 times)
}
//////////////////////////////////////////////////////////////

When I run the perl script it puts up the empty text widget, waits for 12 seconds (the length of time it takes to run the test program) and then inserts all the output into the text widget at once.

Can anyone help me get the output into the text widget in realtime as it comes from the program?




localfilmmaker
stranger

Apr 26, 2001, 12:24 PM

Post #2 of 2 (902 views)
Re: Tk fileevent waits until fork is finished [In reply to] Can't Post

My suggestion is to use the module IPC::Open2, or IPC::Open3. You can create an open pipe to the other program and process its output in realtime. Read more about it at: http://www.perldoc.com/perl5.6/lib/IPC/Open2.html

With open2, you could do something like this:

Code
use Tk; 
use strict;
use IPC::Open2;

local (*RD,*WR);
open2 (\*RD,\*WR, "./program") || die "$!";
close(WR); # this just insures that the program isn't waiting for input
my $fh = \*RD;
my $mw = new MainWindow;
my $text = $mw->Scrolled("Text",
-width => 80,
-height => 25,
-scrollbars => "se")
->pack(-expand => 1);
$mw->fileevent($fh, 'readable', [\&InsertText]);
MainLoop;
sub InsertText {
if (my $line = <$fh>) { # This should now read from the program as it outputs
$text->insert('end', $line);
$text->yview('end');
$text->update;
} else {
$mw->fileevent($fh, 'readable', '');
close($fh);
}
}

-localfilmmaker
Spencer Christensen
spencer@mecworks.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives