E
enstrophy.2000
Hi,
I'm trying to create a perl script for managing multiple tasks. What
I have been doing is creating a queue of tasks via Thread::Queue, and
having two separate threads retrieving
the task names from the queue and execute each one via system() call.
However, upon
running the script, I found that, if I call system() for more than once
after each retrieval of
the task name, only the first system() call actually succeeds in
executing, whereas the
rest got ignored. Even more counterintuitive is that the rest of the
system() calls do return
0, meaning there was a success. Is there anything fundamentally wrong
with this approach?
Any feedback will be greatly appreciated.
Here is the script in question:
#!/usr/bin/perl -w
use threads;
use Thread::Queue;
use Cwd;
$num_thread = 2;
my $q = new Thread::Queue;
#populate this queue using directory names
opendir(DDIR,"..");
@ddir= grep(/^\d/,readdir(DDIR));
foreach $ddir(@ddir){
$q->enqueue($ddir);
}
for (1..$num_thread){
$thr[$_-1] = threads->new(\&sub1,$_);
# print "creating tid ", $thr[$_-1]->tid,"\n";
}
sub sub1 {
my ($id) = @_;
while($foo =$q->dequeue_nb){
batch($foo);
$left = $q->pending;
print "In the thread $id foo=$foo left=$left\n";
last if($left==0);
#sleep($id);
}
foreach $tt(@thr){
print "joining ", $tt->tid,"\n";
$tt->join();
}
sub batch{
my ($ddir) = @_;
chdir "../".$ddir||die "unable to enter $ddir\n";
print "rpt2dat.pl\n";
system("rpt2dat.pl");
print "$ddir R CMD BATCH cmb.obs.mod.R \n";
system("R CMD BATCH cmb.obs.mod.R")==0
or die "$ddir cmb.obs.mod failed $?";
print "$ddir R CMD BATCH comp.ann.sum.R \n";
system("R CMD BATCH comp.ann.sum.R")==0
or die "$ddir cmb.ann.sum failed $?";
print "$ddir R CMD BATCH comp.ann.sum.R finished\n";
wait();
print "$ddir R CMD BATCH comp.mon.sum.R \n";
system("R CMD BATCH comp.mon.sum.R")==0
: or die "$ddir cmb.mon.sum failed $?";
wait();
print "$ddir R CMD BATCH comp.mon.sum.R finished\n";
}
I'm trying to create a perl script for managing multiple tasks. What
I have been doing is creating a queue of tasks via Thread::Queue, and
having two separate threads retrieving
the task names from the queue and execute each one via system() call.
However, upon
running the script, I found that, if I call system() for more than once
after each retrieval of
the task name, only the first system() call actually succeeds in
executing, whereas the
rest got ignored. Even more counterintuitive is that the rest of the
system() calls do return
0, meaning there was a success. Is there anything fundamentally wrong
with this approach?
Any feedback will be greatly appreciated.
Here is the script in question:
#!/usr/bin/perl -w
use threads;
use Thread::Queue;
use Cwd;
$num_thread = 2;
my $q = new Thread::Queue;
#populate this queue using directory names
opendir(DDIR,"..");
@ddir= grep(/^\d/,readdir(DDIR));
foreach $ddir(@ddir){
$q->enqueue($ddir);
}
for (1..$num_thread){
$thr[$_-1] = threads->new(\&sub1,$_);
# print "creating tid ", $thr[$_-1]->tid,"\n";
}
sub sub1 {
my ($id) = @_;
while($foo =$q->dequeue_nb){
batch($foo);
$left = $q->pending;
print "In the thread $id foo=$foo left=$left\n";
last if($left==0);
#sleep($id);
}
foreach $tt(@thr){
print "joining ", $tt->tid,"\n";
$tt->join();
}
sub batch{
my ($ddir) = @_;
chdir "../".$ddir||die "unable to enter $ddir\n";
print "rpt2dat.pl\n";
system("rpt2dat.pl");
print "$ddir R CMD BATCH cmb.obs.mod.R \n";
system("R CMD BATCH cmb.obs.mod.R")==0
or die "$ddir cmb.obs.mod failed $?";
print "$ddir R CMD BATCH comp.ann.sum.R \n";
system("R CMD BATCH comp.ann.sum.R")==0
or die "$ddir cmb.ann.sum failed $?";
print "$ddir R CMD BATCH comp.ann.sum.R finished\n";
wait();
print "$ddir R CMD BATCH comp.mon.sum.R \n";
system("R CMD BATCH comp.mon.sum.R")==0
: or die "$ddir cmb.mon.sum failed $?";
wait();
print "$ddir R CMD BATCH comp.mon.sum.R finished\n";
}