
alex_scheibe
Deleted
Apr 25, 2001, 5:17 AM
Post #1 of 2
(723 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:
############################################################## 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:
////////////////////////////////////////////////////////////// #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?
|