I met to add that MCE::Hobo and MCE::Shared provide capabilities similar to threads and threads::shared respectively. One isn't required to use MCE workers. In fact, MCE::Shared works also with threads, Parallel::ForkManager, and workers spawned with fork.

If using non-MCE workers, then call MCE::Shared->init inside the child. That will spread IPC communication across 10 data channels. The channel assignment is done automatically for threads and MCE workers (i.e. MCE, MCE::Child, and MCE::Hobo).

unless (fork) { # This assigns the worker 1 of 10 data channels in a # round-robin fashion for the life of the worker. MCE::Shared->init(); do_subroutine (); exit; }

Update: Added demonstrations.

Below, please find two demonstrations which one may run for comparison. Parallel data channels are helpful especially when a reply is not needed i.e. incrementing a value or writing to a shared output object. Each worker increments the value 100,000 times.

use strict; use warnings; use MCE::Shared; use Time::HiRes 'time'; my $number = MCE::Shared->scalar(0); sub do_subroutine { $number->incr for 1..1e5; } my @pids; my $start = time; for (1..10) { my $pid; unless ($pid = fork) { do_subroutine (); exit; } push @pids, $pid if defined $pid; } waitpid $_, 0 for @pids; printf "number %d\n", $number->get; printf "seconds %.03f\n", time - $start; __END__ number 1000000 seconds 5.234

That's quite fast considering incrementing the value involves IPC. Faster is possible if you want non-MCE workers to reach levels capable of threads and MCE workers which call MCE::Shared->init automatically.

MCE::Shared->init

Calling MCE::Shared->init is beneficial for non-MCE workers.
use strict; use warnings; use MCE::Shared; use Time::HiRes 'time'; my $number = MCE::Shared->scalar(0); sub do_subroutine { $number->incr for 1..1e5; } my @pids; my $start = time; for (1..10) { my $pid; unless ($pid = fork) { MCE::Shared->init; # enables multi-channel do_subroutine (); exit; } push @pids, $pid if defined $pid; } waitpid $_, 0 for @pids; printf "number %d\n", $number->get; printf "seconds %.03f\n", time - $start; __END__ number 1000000 seconds 2.086

That is shy of 500k per second on Clear Linux.

threads demonstration

For completeness adding threads and MCE::Hobo demonstrations. They benefit from multi-channel IPC automatically and not necessary to call MCE::Shared->init.

use strict; use warnings; use threads; use MCE::Shared; use Time::HiRes 'time'; my $number = MCE::Shared->scalar(0); my $start = time; sub do_subroutine { $number->incr for 1..1e5; } threads->create('do_subroutine') for 1..10; $_->join for threads->list; printf "number %d\n", $number->get; printf "seconds %.03f\n", time - $start; __END__ number 1000000 seconds 2.142

MCE::Hobo demonstration

use strict; use warnings; use MCE::Hobo; use MCE::Shared; use Time::HiRes 'time'; my $number = MCE::Shared->scalar(0); my $start = time; sub do_subroutine { $number->incr for 1..1e5; } MCE::Hobo->create('do_subroutine') for 1..10; $_->join for MCE::Hobo->list; # same as MCE::Hobo->wait_all printf "number %d\n", $number->get; printf "seconds %.03f\n", time - $start; __END__ number 1000000 seconds 2.087

In reply to Re^2: Perl forked processes and variable sharing (update) by marioroy
in thread Perl forked processes and variable sharing by fireblood

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.