Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Why?

Exactly! That's the code in the demo script in the Parallel::ForkManager documentation. It's the example I was running and studying earlier. The PIDs and exit codes are included in the code for didactic reasons.

You'll appreciate my similar demo script based on the same kids-in-line-at-the-pool theme.

use strict; use warnings; use Parallel::ForkManager; $| = 1; my $MAXIMUM_KIDS_IN_POOL = 3; my $MAXIMUM_TIME_IN_POOL = 30; # Seconds my $lifeguard = Parallel::ForkManager->new($MAXIMUM_KIDS_IN_POOL); my @kids = qw( Ann Bob Cal Dan Eve Fay Gus Hal Ike Joe Kim Lee Meg ); $lifeguard->run_on_start( sub { my ($pid, $kid) = @_; print "\nThe lifeguard waves $kid into the pool\n"; } ); $lifeguard->run_on_finish( sub { my ($pid, $exit_code, $kid) = @_; print "\n$kid climbs out of the pool\n"; } ); $lifeguard->run_on_wait( sub { print "."; }, 0.3 ); my $number_of_kids = @kids; print "\nThere are $number_of_kids kids in line at the pool\n\n@kids\n +"; KID_IN_LINE: for my $kid (@kids) { my $pid = $lifeguard->start($kid); next KID_IN_LINE if $pid != 0; srand(); my $time = int(rand($MAXIMUM_TIME_IN_POOL)) + 1; print "\n$kid jumps into the pool\n"; sleep $time; print "\nThe lifeguard orders $kid out of the pool after $time sec +onds\n"; sleep 0.5; $lifeguard->finish(); } print "\nAll the kids have been in the pool\n"; $lifeguard->wait_all_children(); print "\nAll the kids are out of the pool\n"; exit 0;

(As I post this script here, I notice there are vestiges of the original script still in the callback subs; namely, $pid and $exit_code. I could remove them.)

Jim

UPDATE:  And if I'm truly serious about not caring about the PIDs, I could do this instead:

next KID_IN_LINE if $lifeguard->start($kid) != 0;

ANOTHER UPDATE:  More weirdness. This works fine…

$lifeguard->run_on_start( sub { my $kid = pop; print "\nThe lifeguard waves $kid into the pool\n"; } );

…but this blows up with Use of uninitialized value $kid in concatenation (.) or string at Pool.pl line 27

$lifeguard->run_on_finish( sub { my $kid = pop; print "\n$kid climbs out of the pool\n"; } );

No variation of pop()-ing @_ in run_on_finish() works.

YET ANOTHER UPDATE:  OK, this is documented in…ahem…the documentation.


In reply to Re^4: Please Explain the Parallel::ForkManager Idiom my $pid = $pm->start and next; by Jim
in thread Please Explain the Parallel::ForkManager Idiom my $pid = $pm->start and next; by Jim

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (18)
    As of 2014-10-21 12:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (103 votes), past polls