S
Snorik
Hello everyone,
I am trying to speed up a few perl scripts by forking them.
Unfortunately, I need to pass back to the parent.
I am using named pipes at other places, but this time, I wanted to use
shared memory (this being on *nix).
The first case is basically traversing a HUGE directory tree, looking
for certain files and returning them.
The idea is to fork finds from a specific point of the directory tree,
gather all the files in an array for each child process and then store
that array as reference as value of the hash.
For this, I have done:
sub get_fbas_for_rg
{
my $rg = shift;
my @children;
use IPC::Shareable;
use Data:umper;
use constant MYGLUE => 'Test';
my %fba_hash;
my $handle = tie (%fba_hash, IPC::Shareable, MYGLUE, {create =>
1, mode => 0666}) or die "cannot tie to shared memory: $! \n";
my @ggs = qx (ls /default/main/www/$rg | grep -v STAGING | grep -
v WORKAREA | grep -v EDITION);
foreach my $gg (@ggs)
{
chomp $gg;
my $gg_fba = $gg."_FBAs";
my $pid = fork();
if ($pid)
{
push(@children, $pid);
}
elsif ($pid == 0)
{
my @fbas = (qx (/usr/bin/find /default/main/
www/$rg/$gg/WORKAREA/workarea/$gg_fba -type f ));
$handle->shlock();
push (@{$fba_hash{$gg}}, @fbas);
$handle->shunlock();
exit (0);
}
else
{
print STDERR "\nERROR: fork failed: $!\n";
}
}
foreach (@children)
{
waitpid($_, 0);
}
return %fba_hash;
}
Now, If I call this function, it seems to work fine, only the hash
values contain only the scalars of the array, at least that is what
Data Dumper tells me:
$VAR1 = {
'dir1' => 3858,
'dir2' => 2394,
'dir3' => 2075
};
This is what I do in the script:
my %fbas = TestPackage::get_fbas_for_rg("test");
print Dumper \%fbas;
foreach my $gg (keys %fbas)
{
print $gg."\n";
foreach my $fba (sort @{$fbas{$gg}})
{
print $fba."\n";
}
}
The foreach loop does not return anything (understandable since the
hash value only contains the scalar of the array).
Again, my question is: how do I manage to receive the actual array in
the calling script instead of just a hash containing my designated
keys and the sizes of the arrays as values?
I would be very grateful for some help.
I am trying to speed up a few perl scripts by forking them.
Unfortunately, I need to pass back to the parent.
I am using named pipes at other places, but this time, I wanted to use
shared memory (this being on *nix).
The first case is basically traversing a HUGE directory tree, looking
for certain files and returning them.
The idea is to fork finds from a specific point of the directory tree,
gather all the files in an array for each child process and then store
that array as reference as value of the hash.
For this, I have done:
sub get_fbas_for_rg
{
my $rg = shift;
my @children;
use IPC::Shareable;
use Data:umper;
use constant MYGLUE => 'Test';
my %fba_hash;
my $handle = tie (%fba_hash, IPC::Shareable, MYGLUE, {create =>
1, mode => 0666}) or die "cannot tie to shared memory: $! \n";
my @ggs = qx (ls /default/main/www/$rg | grep -v STAGING | grep -
v WORKAREA | grep -v EDITION);
foreach my $gg (@ggs)
{
chomp $gg;
my $gg_fba = $gg."_FBAs";
my $pid = fork();
if ($pid)
{
push(@children, $pid);
}
elsif ($pid == 0)
{
my @fbas = (qx (/usr/bin/find /default/main/
www/$rg/$gg/WORKAREA/workarea/$gg_fba -type f ));
$handle->shlock();
push (@{$fba_hash{$gg}}, @fbas);
$handle->shunlock();
exit (0);
}
else
{
print STDERR "\nERROR: fork failed: $!\n";
}
}
foreach (@children)
{
waitpid($_, 0);
}
return %fba_hash;
}
Now, If I call this function, it seems to work fine, only the hash
values contain only the scalars of the array, at least that is what
Data Dumper tells me:
$VAR1 = {
'dir1' => 3858,
'dir2' => 2394,
'dir3' => 2075
};
This is what I do in the script:
my %fbas = TestPackage::get_fbas_for_rg("test");
print Dumper \%fbas;
foreach my $gg (keys %fbas)
{
print $gg."\n";
foreach my $fba (sort @{$fbas{$gg}})
{
print $fba."\n";
}
}
The foreach loop does not return anything (understandable since the
hash value only contains the scalar of the array).
Again, my question is: how do I manage to receive the actual array in
the calling script instead of just a hash containing my designated
keys and the sizes of the arrays as values?
I would be very grateful for some help.