Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: IPC:Shareable: Not an array reference

by Bloehdian (Beadle)
on Oct 11, 2016 at 03:54 UTC ( [id://1173693]=note: print w/replies, xml ) Need Help??


in reply to Re^2: IPC:Shareable: Not an array reference
in thread IPC:Shareable: Not an array reference

Hello Mario,

THX for the reply.

I checked the documentation on CPAN, but it is not totally clear to me, how I would implement locking with this module. In the actual application, both processes will manipulate @data.

Cheers

Bloehdian

  • Comment on Re^3: IPC:Shareable: Not an array reference

Replies are listed 'Best First'.
Re^4: IPC:Shareable: Not an array reference
by marioroy (Prior) on Oct 11, 2016 at 06:52 UTC

    MCE::Shared provides a single point of entry for commands sent to the shared manager process no matter the number of workers. This allows zero locking at the application level. The multiple data channels behind MCE::Shared is for reducing IPC latency. With that in mind, accessing the shared data is better done via the OO methods. The TIE interface may require locking and possible with MCE::Mutex.

    OO interface.

    use strict; use warnings; use MCE::Shared; my $data = MCE::Shared->array(); if ( ! defined ( my $pid = fork() ) ) { die "Cannot fork!: $!"; } elsif ( $pid == 0 ) { # Child MCE::Shared->init(); my $item; sleep 1; for ( my $i = 1; $i <= 10; $i++ ) { $item = $data->shift(); $item = '' if ( ! defined $item ); print "Child: $item\n"; } } else { # Parent for ( my $i = 1; $i <= 10; $i++ ) { $data->push($i); print "Parent: ", join(" ", $data->vals()), "\n"; } waitpid( $pid, 0 ); }

    OO interface with locking.

    use strict; use warnings; use MCE::Mutex; use MCE::Shared; my $mutex = MCE::Mutex->new(); my $data = MCE::Shared->array(); if ( ! defined ( my $pid = fork() ) ) { die "Cannot fork!: $!"; } elsif ( $pid == 0 ) { # Child MCE::Shared->init(); my $item; sleep 1; for ( my $i = 1; $i <= 10; $i++ ) { $mutex->lock(); $item = $data->shift(); $mutex->unlock(); $item = '' if ( ! defined $item ); print "Child: $item\n"; } } else { # Parent for ( my $i = 1; $i <= 10; $i++ ) { $mutex->lock(); $data->push($i); $mutex->unlock(); print "Parent: ", join(" ", $data->vals()), "\n"; } waitpid( $pid, 0 ); }

    OO interface (de-referencing) with locking.

    use strict; use warnings; use MCE::Shared; my $data = MCE::Shared->array(); if ( ! defined ( my $pid = fork() ) ) { die "Cannot fork!: $!"; } elsif ( $pid == 0 ) { # Child MCE::Shared->init(); my $item; sleep 1; for ( my $i = 1; $i <= 10; $i++ ) { $item = shift @$data; $item = '' if ( ! defined $item ); print "Child: $item\n"; } } else { # Parent for ( my $i = 1; $i <= 10; $i++ ) { push @$data, $i; print "Parent: @$data\n"; } waitpid( $pid, 0 ); }

    Summary.

    Locking may be omitted at the application level for the OO interface including de-referencing unless wanting to call multiple OO methods. Furthermore, MCE::Shared::{ Array, Hash, Ordhash, and Scalar } provide sugar methods resembling the Redis API without having to call set and get explicitly.

    # Locking is necessary via the TIE interface. # Below, set and get are called separately behind the scene ( 2 IPC st +atements ). my $m1 = MCE::Mutex->new(); tie my @a1, 'MCE::Shared'; $m1->lock; my $v0 = $a1[0] = "item0"; $m1->unlock; # Locking is optional at the application level when using the OO inter +face. # This is made possible by the single point of entry. my $a2 = MCE::Shared->array(); my $v2 = $a2->set(0, "item2"); my $a3 = MCE::Shared->array(); my $v3 = $a3->[0] = "item3";

    Regards, Mario.

Re^4: IPC:Shareable: Not an array reference
by marioroy (Prior) on Nov 01, 2016 at 22:19 UTC

    Hello Bloehdian,

    Re: I checked the documentation on CPAN, but it is not totally clear to me, how I would implement locking with this module.

    MCE::Shared 1.806, released today, contains a new section titled LOCKING in the documentation.

    Regards, Mario.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1173693]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-24 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found