Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-28 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found