Re: IPC::Shareable Problem with multidimentional hash

Sébastien_Cottalorda <scottalorda@xxxxxxxxxxx> wrote:
> xhoster@xxxxxxxxx wrote:
> > Sébastien_Cottalorda <scottalordaNOSPAM@xxxxxxxxxxx> wrote:
> >
> >>Hi all,
> >>
> >>I need to use that kind of shared hash:
> >>%shared_hash = (
> >> '123456' => {
> >> 'bla' => 123,
> >> 'bli' => 465,
> >> ect ...
> >> },
> >> ect ...
> >>);
> >>
> >>with about 500 primary keys.
> >>When I "feed" the %shared_hash, I note that the system create as many
> >>shared segment and semaphore than there are primary keys.
> >
> >
> > Yes. This fact is documented.
> Where ... ?
> I understand limits but not the manner it stores datas like hashes.

It is not the hashes, it is the references to the hashes:


When a reference to a non-tied scalar, hash, or array is assigned to a
tie()d variable, IPC::Shareable will attempt to tie() the thingy being
referenced[4] ....
Secondly, since a new shared memory segment is created for each thingy
being referenced, the liberal use of references could cause the system to
approach its limit for the total number of shared memory segments allowed.

> >
> > You could try using Storable or Data::Dumper to collapse the inner
> > structures into flat strings for storage.
> >
> > Xho
> I've read, re-read, re-re-read the perldoc IPC::Shareable : It seems to
> already use Storable to store hash structure.

It uses Storable to store the top level hash structure. It also uses
Storable to store each sub-hash too, but it does each of those in a
different shmem segment. If you preemptively use Storable on the inner
hashes, so that they are just strings and not hashrefs by the time they get
stuck in the main hash, then IPC::Shareable would just store them as
strings in the main shmem segment.

> Concerning Data::Dumper, I don't understand anything (maybe it's me ;-) )
> I don't understand how I can use that module to store and manipulate
> hash of hash in a share structure.

You use Data::Dumper the same way you use Storable, but the thawing is
more awkward.

> If you have an exemple, I'll apreciate a lot.

It ain't pretty, but I don't think using shared memory ever is pretty.

my ($i,$j);
for $i (1 .. 300){
my %x;
for $j (1..3){
$x{$j} = "fall";
$colours{$i} = freeze \%x;
print "Charged - Press a key to continue -\n";
my $e=<STDIN>;
foreach $i ( keys %colours){
print "$i => \n";
my $x=thaw $colurs{$i};
foreach $j (keys %$x){
print " $x{$j}\n";

Note that this won't be very fast, because it is freezing and thawing the
entire data structure just to access one part of it. I suspect that that
is why Shareable was implemented the way it was (using seperate segments
for each piece) in the first place.

Shared memory is not one of the hard things which Perl has made easy.


-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB