Update: Added Foo::Inbox4 and removed Foo::Inbox3. I've been accustomed to automatic serialization of complex data structure in MCE::Shared that I didn't realize on having to do that manually for Thread::Queue.

Corion mentioned queues. The following provides two queue implementations based on Foo::Inbox.

Foo::Inbox2 using MCE::Shared->queue

package Foo::Inbox2; use strict; use warnings; our $VERSION = '0.003'; use MCE::Shared; # $inbox = Foo::Inbox->new(); sub new { my ( $class, @names ) = @_; my %self = map { $_ => MCE::Shared->queue( fast => 1 ) } @names; MCE::Shared->start() unless ( exists $INC{'IO/FDPass.pm'} ); bless \%self, $class; } # $scalar = $inbox->size( [ $key ] ); # %pairs = $inbox->size(); sub size { my ( $self, $key ) = @_; if ( defined $key ) { exists $self->{$key} ? $self->{$key}->pending() : 0; } elsif ( wantarray ) { local $_; map { $_ => $self->{$_}->pending() } keys %{ $self }; } else { my $size = 0; foreach my $key ( keys %{ $self } ) { $size += $self->{$key}->pending(); } $size; } } # $inbox->send( $from, $to, $arg1, ... ); # $inbox->send( $from, \@list, $arg1, ... ); sub send { my ( $self, $from, $to ) = ( shift, shift, shift ); my $mesg = [ $from, [ @_ ] ]; if ( ref $to eq 'ARRAY' ) { $self->{$_ }->enqueue($mesg) for @{ $to }; } else { $self->{$to}->enqueue($mesg); } return; } # $inbox->recv( $from ); sub recv { my ( $self, $from ) = @_; return () unless exists $self->{$from}; @{ $self->{$from}->dequeue() // [] }; } # $inbox->end(); sub end { my ( $self ) = @_; foreach my $from ( values %{ $self } ) { $from->end(); } return; } 1;

Foo::Inbox4 using Thread::Queue

package Foo::Inbox4; use strict; use warnings; our $VERSION = '0.003'; use Thread::Queue; my ( $freeze, $thaw ); BEGIN { if ( !exists $INC{'PDL.pm'} ) { eval ' use Sereal::Encoder 3.015 qw( encode_sereal ); use Sereal::Decoder 3.015 qw( decode_sereal ); '; if ( !$@ ) { my $encoder_ver = int( Sereal::Encoder->VERSION() ); my $decoder_ver = int( Sereal::Decoder->VERSION() ); # ensure the base version match e.g. 3 if ( $encoder_ver - $decoder_ver == 0 ) { $freeze = sub { encode_sereal( @_, { freeze_callbacks => 1 } ) + }, $thaw = \&decode_sereal; } } } if ( !defined $freeze ) { require Storable; $freeze = \&Storable::freeze, $thaw = \&Storable::thaw; } } # $inbox = Foo::Inbox->new(); sub new { my ( $class, @names ) = @_; my %self = map { $_ => Thread::Queue->new() } @names; bless \%self, $class; } # $scalar = $inbox->size( [ $key ] ); # %pairs = $inbox->size(); sub size { my ( $self, $key ) = @_; if ( defined $key ) { exists $self->{$key} ? $self->{$key}->pending() : 0; } elsif ( wantarray ) { local $_; map { $_ => $self->{$_}->pending() } keys %{ $self }; } else { my $size = 0; foreach my $key ( keys %{ $self } ) { $size += $self->{$key}->pending(); } $size; } } # $inbox->send( $from, $to, $arg1, ... ); # $inbox->send( $from, \@list, $arg1, ... ); sub send { my ( $self, $from, $to ) = ( shift, shift, shift ); my $mesg = $freeze->( [ $from, [ @_ ] ] ); if ( ref $to eq 'ARRAY' ) { $self->{$_ }->enqueue($mesg) for @{ $to }; } else { $self->{$to}->enqueue($mesg); } return; } # $inbox->recv( $from ); sub recv { my ( $self, $from ) = @_; return () unless exists $self->{$from}; my $mesg = $self->{$from}->dequeue(); $mesg ? @{ $thaw->($mesg) } : (); } # $inbox->end(); sub end { my ( $self ) = @_; foreach my $from ( values %{ $self } ) { $from->end(); } return; } 1;

A demo and benchmark will follow in the immediate post(s).

Regards, Mario


In reply to Re: Child process inter communication by marioroy
in thread Child process inter communication by smarthacker67

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.