Reputation: 31
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?
Upvotes: 1
Views: 1938
Reputation: 118128
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: $?";
Upvotes: 1
Reputation: 36402
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
Upvotes: 2
Reputation: 132783
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.
Upvotes: 4