raxor has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm writing a multi-threaded web spider. I've written a threading pool with a dispatcher for processes which works very well.
My problem is getting the children to modify a global array. I've tried using the IPC::Shareable module, with no avail. Then i tried this IPC::ShareLite. I use it exactly as documented, and I'm getting an error. Maybe someone can help me out with this.
Here's some code:
use IPC::ShareLite; use Storable qw(freeze thaw); use POSIX qw(:sys_wait_h); my $share = new IPC::ShareLite( -key => 'link', -create => 'yes', -destroy => 'yes', -size => 262144, -mode => 0660, ) or die "Cannot create segment: $!"; my $arrayref; # the array i want to share with my children push @{$arrayref}, 'some value'; $share->store(freeze($arrayref) ); if ($pid = fork()) { #************* parent ++$num_procs; ++$flag; print "$num_procs processes running\n"; } elsif (defined $pid) { #************* child my $alllinks; my @temp_array; $alllinks = thaw( $share->fetch() ); # fetch the global array +from memory THIS IS THE LINE THAT'S GIVING ME AN ERROR foreach $blah (@hrefs) { $blah = url($blah, $base)->abs; if ($blah =~ m/^http:(.*)[|\.]$localdomain[\/|:|]/i) { $found = 0; for (@{$alllinks}) { (split /^/, $_)[0] eq $blah and $found++, last } if ($found == 0) { push @temp_array, $blah; } } } $share->lock("LOCK_EX"); # obtain an exclusive lock on the sh +ared memory segment push @{$alllinks}, @temp_array; # modify the global array $share->store( freeze($alllinks)); # store it back into shared + memory $share->lock("LOCK_UN"); # unlock it exit; # always exit the child } else { # can't fork }
Basically that's all it does. When i execute it, It forks one child and dies on this line
$alllinks = thaw( $share->fetch() );
Saying : IPC::ShareLite fetch() error: Invalid argument at myscript.pl line 254

Has anyone used this module before ? Successfully ?
Please give me some insight, I'm pulling my hair here.


Show me some sample scripts, anything.
Thanks in advance for any input.

- DAFT PUNK ARE ROBOTS

Replies are listed 'Best First'.
Re: Problems with IPC::ShareLite
by MZSanford (Curate) on Jul 05, 2001 at 11:46 UTC

    I have not used IPC::ShareLite, and am at home without a Unix machine to test, but after looking through the module, it appears the invalid arg message is caused by a return of undef from read_shre (the XS function).

    as i said, i have not tested this, but here is what i am seeing :
    1. fetch (SharLite.pm) calls read_share
    2. read_share (ShareLite.xs) calls system function, and return string, or undef if string length is <= 0
    3. fetch (ShareLite.pm) error if return from read_share is undef

    Based on this, i would guess that if fetch() is being called prior to adding data to the share, it could possibly error ... but, thats just a guess.


    may the foo be with you
      So what would be a way of solving this problem ?