E
Eric
Hello,
I was given a task that requires me to launch multiple executions of
the same script using different args as input. Instead of waiting for
the first to complete before starting the second one, I need to run
them in their own thread (i.e. parallel).
Here is what I've done so far. The script I am executing is called
'log_on_off.exp', which is an Expect script that logs on, then off of
a console, which is provided as input to the command:
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
use Config;
use threads;
my @cmdline = ("log_on_off.exp mach003-con",
"log_on_off.exp mach022-con",
"log_on_off.exp mach030-con");
foreach (@cmdline) {
my $thr = threads->new(\&runExpectScript, $_);
}
sub runExpectScript {
my $cmd = $_;
my $runScript = `$cmd`;
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
After running this script, I can see that all three executions are
running as separate processes, which is what I would expect:
$ ps
PID TTY TIME CMD
2150 pts/1 00:00:23 bash
30151 pts/1 00:00:00 log_on_off.exp
30153 pts/1 00:00:00 log_on_off.exp
30155 pts/1 00:00:00 log_on_off.exp
30268 pts/1 00:00:00 ps
$
It turns out that I need the result of the subroutine to determine if
the login, logout was successful. So I tried using the join function
as follows:
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
use Config;
use threads;
my @cmdline = ("log_on_off.exp mach003-con",
"log_on_off.exp mach022-con",
"log_on_off.exp mach030-con");
foreach (@cmdline) {
my $thr = threads->new(\&runExpectScript, $_);
my $retResponse = $thr->join;
}
sub runExpectScript {
my $cmd = $_;
my $runScript = `$cmd`;
if ($runScript =~ m/successful/) {
print "SUCCESS\n";
} else {
print "FAILURE\n";
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
When I run this Perl script in the background, I never see more than
one log_on_off.exp process running at any time. According to the
document I am using as a learning tool (http://perldoc.perl.org/
perlthrtut.html), the join function waits for the thread to exit
before continuing. I interpret this as saying that the second command
will not be started until the first returns, which defeats the purpose
of my using Perl threads to begin with, since these commands are being
executed serially (which is no different than running them in a loop
without even using Perl threads). Is my interpretation correct?
Is there any way I can execute these commands and parse the input in
their own thread instead of waiting for the previous command to
finish? Of course, if this can be done, I need to be able to determine
what thread returned what value.
Thanks in advance to all that respond.
Eric
I was given a task that requires me to launch multiple executions of
the same script using different args as input. Instead of waiting for
the first to complete before starting the second one, I need to run
them in their own thread (i.e. parallel).
Here is what I've done so far. The script I am executing is called
'log_on_off.exp', which is an Expect script that logs on, then off of
a console, which is provided as input to the command:
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
use Config;
use threads;
my @cmdline = ("log_on_off.exp mach003-con",
"log_on_off.exp mach022-con",
"log_on_off.exp mach030-con");
foreach (@cmdline) {
my $thr = threads->new(\&runExpectScript, $_);
}
sub runExpectScript {
my $cmd = $_;
my $runScript = `$cmd`;
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
After running this script, I can see that all three executions are
running as separate processes, which is what I would expect:
$ ps
PID TTY TIME CMD
2150 pts/1 00:00:23 bash
30151 pts/1 00:00:00 log_on_off.exp
30153 pts/1 00:00:00 log_on_off.exp
30155 pts/1 00:00:00 log_on_off.exp
30268 pts/1 00:00:00 ps
$
It turns out that I need the result of the subroutine to determine if
the login, logout was successful. So I tried using the join function
as follows:
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
use Config;
use threads;
my @cmdline = ("log_on_off.exp mach003-con",
"log_on_off.exp mach022-con",
"log_on_off.exp mach030-con");
foreach (@cmdline) {
my $thr = threads->new(\&runExpectScript, $_);
my $retResponse = $thr->join;
}
sub runExpectScript {
my $cmd = $_;
my $runScript = `$cmd`;
if ($runScript =~ m/successful/) {
print "SUCCESS\n";
} else {
print "FAILURE\n";
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
When I run this Perl script in the background, I never see more than
one log_on_off.exp process running at any time. According to the
document I am using as a learning tool (http://perldoc.perl.org/
perlthrtut.html), the join function waits for the thread to exit
before continuing. I interpret this as saying that the second command
will not be started until the first returns, which defeats the purpose
of my using Perl threads to begin with, since these commands are being
executed serially (which is no different than running them in a loop
without even using Perl threads). Is my interpretation correct?
Is there any way I can execute these commands and parse the input in
their own thread instead of waiting for the previous command to
finish? Of course, if this can be done, I need to be able to determine
what thread returned what value.
Thanks in advance to all that respond.
Eric