See the Parallel::ForkManager demonstration, here. Something similar can be done with MCE::Child. Subsequently, MCE. Notice the PIDs in the 2nd demonstration where the workers persist.

MCE::Child

use strict; use warnings; use MCE::Child qw( ); use Time::HiRes qw( time ); my @testLists = qw( sun moon wind air ); my $opts = 'foo'; my $status = 0; sub regressions { my ($opts, $list) = @_; print "$$: $list\n"; sleep 1; # simulate processing return 1; } my $start = time(); MCE::Child->init( max_workers => 2, on_finish => sub { my ( $pid, $exit_code, $ident, $exit_signal, $core_dump, $data ) + = @_; if ( $exit_code || $exit_signal || $core_dump ) { # Handle error. } $status += $data->{ status }; } ); for my $list ( @testLists ) { MCE::Child->create(sub { my $status = regressions( $opts, $list ); return { status => 1 }; }); } MCE::Child->wait_all(); print "status: $status\n"; printf "duration: %0.3f seconds\n", time() - $start; __END__ 27481: sun 27482: moon 27483: wind 27484: air status: 4 duration: 2.013 seconds

MCE

use strict; use warnings; use MCE::Loop qw( ); use Time::HiRes qw( time ); my @testLists = qw( sun moon wind air ); my $opts = 'foo'; my $status = 0; sub regressions { my ($opts, $list) = @_; print "$$: $list\n"; sleep 1; # simulate processing return 1; } my $start = time(); MCE->new( max_workers => 2, chunk_size => 1, input_data => \@testLists, gather => sub { $status += $_[0] }, user_func => sub { my $list = $_; my $status = regressions( $opts, $list ); MCE->gather( $status ); }, )->run(); print "status: $status\n"; printf "duration: %0.3f seconds\n", time() - $start; __END__ 27562: sun 27563: moon 27562: wind 27563: air status: 4 duration: 2.005 seconds

In reply to Re^3: running a function for different list parallely using fork - MCE::Child, MCE by marioroy
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.