P::FM forks a child for each "task". If you provide a limit (e.g. the 2), it waits for children to finish before forking if forking would bring the number of children over the limit.

use v5.14; use warnings; use Parallel::ForkManager qw( ); use Time::HiRes qw( sleep ); my $pm = Parallel::ForkManager->new ( 2 ); for ( 1 .. 9 ) { $pm->start and next; say "[$_] start"; sleep( $_ == 1 ? 4 : 0.3 ); say "[$_] done"; $pm->finish; } $pm->wait_all_children();

Annotated output:

1 2 3 4 5 6 7 8 9 ----- --- --- --- --- --- --- --- --- --- [1] start 1 XXX [2] start 1 & 2 XXX XXX [2] done 1 XXX [3] start 1 & 3 XXX XXX [3] done 1 XXX [4] start 1 & 4 XXX XXX [4] done 1 XXX [5] start 1 & 5 XXX XXX [5] done 1 XXX [1] done [6] start 6 XXX [7] start 6 & 7 XXX XXX [6] done 7 XXX [7] done [8] start 8 XXX [9] start 8 & 9 XXX XXX [8] done 9 XXX [9] done

In reply to Re^4: running a function for different list parallely using fork by ikegami
in thread running a function for different list parallely using fork by noviceuser

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.