tags:

views:

466

answers:

3

I am creating a Perl/TK GUI code that will call a seperate exe inside. The progress bar widget will be the only indication that execution is happening inside it but problem is, as you run the code, the progress bar freezes because it has to finish first the execution of the seperate exe and after it's done, activity on the progress can be updated.

Is there a better way to have a simultenous implementation of the progress with respect to the seperate exe so as to have the real time execution of the code?

+4  A: 

How are you calling the external program? If you are blocking on something like system(), don't do that. Run the program in a separate process then poll to check if it is running. As long as it is running, you update your progress indicator.

brian d foy
+1. Search SO for recent questions about non-blocking execution of sub-processes in Perl.
Andrew Barnett
Hi Brian,I am calling the external program through system(). Can you please elaborate more on your concept?Thanks!
newbee_me
+2  A: 

Do you get output for progress from the other process? if so, you can use open() to run your subprocess.

open(MYSUBPROC, '-|', "myprocess args") or die "could not execute!";
while (<MYSUBPROC>)
{
    #do something...
}
close(MYSUBPROC) || warn "subproc returned error $?";

You should also take a look at the perlipc sections on using open() for IPC, and Safe pipe opens

Hasturkun
+1  A: 

I don't know enough Perl/Tk to whip up a quick example.

worker.pl

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

for (0 .. 10) {
    print 10 * $_, "\n";
    sleep 1;
}

boss.pl

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

open my $worker_h, '-|', 'worker.pl'
    or die "Cannot open pipe to worker: $!";

while ( my $status = <$worker_h> ) {
    chomp $status;
    my $ticks = 2 * ($status/10);
    print '=' x $ticks, "\r" x $ticks;
    # do other stuff;
}

close $worker_h
    or die "Error closing pipe to worker: $?";
Sinan Ünür