Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

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

Update: Added next statement to not delay unless the inbox is empty.

The following is a demonstration using Parallel::ForkManager + MCE::Shared + Foo::Inbox.

use strict; use warnings; use Foo::Inbox; use Parallel::ForkManager; use MCE::Shared; use List::Util 'shuffle'; use Time::HiRes 'sleep'; my $inbox = MCE::Shared->share( Foo::Inbox->new() ); my @names = shuffle qw/ Barny Betty Fred Wilma /; my $index = 0; $| = 1; sub foo { my $name = shift; my $count = 0; # remove my name from the list @names = grep { $_ ne $name } shuffle @names; # send greeting to names on the list $inbox->send($name, \@names, 'Hello'); while ( 1 ) { if ( my ($from, $data) = $inbox->recv($name) ) { # so soon, alrighty then ;-) last if $data->[0] eq 'END'; # display the message received printf "%-5s received %s from %s\n", $name, $data->[0], $from; # forward the message to another worker $inbox->send($name, $names[ ++$index % @names ], $data->[0]) if ( $from eq 'manager' ); next; } sleep 0.01; } } my $pm = new Parallel::ForkManager(scalar @names); $pm->set_waitpid_blocking_sleep(0); foreach my $name ( @names ) { $pm->start($name) and next; # Have the shared-manager assign a data channel for IPC. # This is done automatically for MCE, MCE::Hobo, and threads. MCE::Shared->init(); # Enter the foo routine. foo($name); $pm->finish($name); } # Enter message or type quit to terminate the script. while ( my $msg = <STDIN> ) { chomp $msg; next unless ( length $msg ); $inbox->end(), last() if ( $msg eq 'quit' ); $inbox->send('manager', $names[ ++$index % @names ], $msg); } $pm->wait_all_children;

MCE::Signal handles signal handling behind the scene if one were to press Ctrl-C (SIGINT). Ditto for the process receiving SIGQUIT or SIGTERM. What is nice about this arrangement is that MCE::Shared and MCE::Signal (loaded by MCE::Shared::Server) complements Parallel::ForkManager quite nice ;-).

For SIGPIPE to work reliably, it requires $| = 1 on STDOUT. Then the following is possible on any Unix platforms. The script terminates gracefully including the tempdir removal inside Parallel::ForkManager.

perl script.pl | head -5

Regards, Mario


In reply to Re^4: 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 surveying the Monastery: (4)
As of 2024-04-20 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found