in reply to Code brewing for the upcoming MCE 10 year anniversary

I completed usability testing,... The following is a demonstration running MCE in the foreground and background. The spawn keyword is also used for spinning a child process per each element.

use MCE::Simple -strict, ( spawn_limit => 2, # MCE::Child processes max_workers => 2, # number of MCE workers init_relay => 1, # define to enable MCE::relay ); # Run MCE in the foreground. mce_foreach my $i (10..18) { MCE::relay { # output orderly say "$$: $i"; }; } # Run MCE in the background. spawn sub { mce_foreach my $i (20..28) { MCE::relay { say "$$: $i"; }; } }; sync; # Spin a worker per each input element. # Up to "spawn_limit" will run simultaneously. # Blocking otherwise, until next availability. spawn my $i (30..38) { say "$$: $i"; } sync;

output:

60264: 10 # alternating PIDs 60265: 11 # two MCE workers 60264: 12 60265: 13 60264: 14 60265: 15 60264: 16 60265: 17 60264: 18 60267: 20 # alternating PIDs 60268: 21 # two MCE workers 60267: 22 60268: 23 60267: 24 60268: 25 60267: 26 60268: 27 60267: 28 60269: 30 # spawn keyword 60270: 31 # unique PIDs 60271: 32 60272: 33 60273: 34 60274: 35 60275: 36 60276: 37 60277: 38

spawn keyword:

spawn my $i (30..38) { say "$$: $i"; } # filtered/transposed to this, without altering line no. foreach my $i (30..38) { MCE::Simple::_spawn_a('', sub { say "$$: $i"; }); }

Replies are listed 'Best First'.
Re^2: Code brewing for the upcoming MCE 10 year anniversary
by marioroy (Prior) on Nov 11, 2022 at 12:15 UTC

    Update: Changed to "spawn" without the "_each" suffix. Thank you, choroba.

    I updated the prior example. Seeing "spawneach" made me rename it to "spawn_each". I can leave out the "_each" suffix, but preferred to be clear that "spawn_each" will be spinning a child process per each element.

    # serial code foreach my $i (30..38) { say "$$: $i"; } # this seems clear? spawn_each my $i (30..38) { say "$$: $i"; } # will this confuse folks? spawn my $i (30..38) { say "$$: $i"; }

    Which one do you prefer (spawn_each, spawn) or something else? The "spawn" without the "_each" suffix is elegant, but wonder if folks will think not spawning a child process per element.

    MCE::Simple is quite nice. It does not alter the line count when filtering / transposing to parallel code.

      Hi Mario

      Maybe spawn_for_each ... ?


      The way forward always starts with a minimal test.

        Yesterday, I had code supporting spawn foreach and for.

        spawn foreach my $i (30..38) { say "$$: $i"; } spawn for my $i (30..38) { say "$$: $i"; }