Greetings, all

The following is a glimpse of what's coming for MCE. There are two new modules; MCE::Semaphore and MCE::Simple. I completed the code tonight. Now, I need to finish the docs and more testing before releasing on Meta::CPAN.

MCE Simple

use MCE::Simple -strict, max_workers => 4; MCE::Simple->init( # mce options user_begin => sub { MCE->say("hello from ", MCE->wid); }, # spawn options on_finish => sub { my ( $pid, $exit, $ident, $signal, $error, @ret ) = @_; say "@_"; }, ); mce_foreach my $i ( 1..10 ) { MCE->say(MCE->wid, ": $i * 2 = ", $i * 2); } spawn "Hello", sub { "one" }; spawn "There", sub { "two" }; foreach my $ident (qw/foo baz/) { spawn $ident, sub { my $text = "something from $$"; }; } sync; # clear or set options MCE::Simple->init(); sub fib { my $n = shift; return $n if $n < 2; spawn my $x = fib($n - 1); spawn my $y = fib($n - 2); sync $x; sync $y; return $x + $y; } say "fib(20) = ", fib(20);

Output

$ perl demo.pl hello from 1 hello from 2 hello from 3 hello from 4 3: 2 * 2 = 4 4: 1 * 2 = 2 2: 3 * 2 = 6 3: 5 * 2 = 10 4: 6 * 2 = 12 1: 4 * 2 = 8 4: 7 * 2 = 14 3: 8 * 2 = 16 2: 9 * 2 = 18 1: 10 * 2 = 20 10792 0 Hello 0 one 10793 0 There 0 two 10794 0 foo 0 something from 10794 10795 0 baz 0 something from 10795 fib(20) = 6765

MCE Semaphore

A fast pure-Perl implementation. Testing involves big number. Notice the mce_foreach_s keyword, for processing a sequence of numbers.

use MCE::Simple -strict, max_workers => 16; use MCE::Semaphore; use Time::HiRes 'time'; my $start = time; my $sem = MCE::Semaphore->new(8); mce_foreach_s ( 1 .. 1_000_000 ) { $sem->down; $sem->up; } printf "%0.3f seconds\n", time - $start;

Input file

mce_foreach_f ("/path/to/file") { MCE->print($_); } # what about file handles? no problem... open my $fh, "<", "/tmp/infile.txt" or die "open error: $!"; mce_foreach_f my $line ( $fh ) { MCE->print($line); } close $fh;

In reply to Code brewing for the upcoming MCE 10 year anniversary by marioroy

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.