in reply to Re: Why should any one use/learn Perl 6?
in thread Why should any one use/learn Perl 6?
Parallelism: Doing this right in Perl 5 needs a lot of boilerplate ...
Fake news!
See MCE and MCE::Shared.
Consider the following utility script to delete records from a customer account in a global cloud services provider using their API:
use strict; use warnings; use feature 'say'; use Data::Dumper; use MyClient; my $client = MyClient->new; my $list = $client->call('ListCustomers'); $list->parse_json_payload; say sprintf 'Start: Found %d accounts.', $list->payload->{totalCount}; my %results; for my $account ( @{ $list->payload->{items} } ) { my $id = $account->{id}; say "Deleting $id"; my $call = $client->call('DeleteCustomer', { account_id => $id }); say 'Status: ' . $call->status; $call->status == 204 ? $results{OK}++ : $results{NOT_OK}++; } say 'Results: ' . Dumper \%results; __END__
Now, parallelized with MCE. Too much boilerplate?
use strict; use warnings; use feature 'say'; use Data::Dumper; use MyClient; use MCE; use MCE::Shared; my $client = MyClient->new; my $list = $client->call('ListCustomers'); $list->parse_json_payload; say sprintf 'Start: Found %d accounts.', $list->payload->{totalCount}; tie my %results, 'MCE::Shared'; sub task { my $id = $_->{id}; my $output = "Deleting $id\n"; my $call = $client->call('DeleteCustomer', { account_id => $id }); say $output . 'Status: ' . $call->status; $call->status == 204 ? $results{OK}++ : $results{NOT_OK}++; } MCE->new( chunk_size => 1, max_workers => 7, user_func => \&task ); MCE->process( $list->payload->{items} ); MCE->shutdown; say 'Results: ' . Dumper \%results; __END__
Note that in the second version, we can still just use $call->status == 204 ? $results{OK}++ : $results{NOT_OK}++; even though we have 7 workers writing to the same hash.
(Also note that MCE provides comprehensive signal handling, so if you use an END block (not shown here) to print out your results hash (for example), you can kill the script eg with CTRL-C, and all workers will exit gracefully, while only the parent prints out the data so far.)
You sure as hell don't need Cuckoo, errr, notPerl "6", or anything other than Perl to write clean, fast, correct, parallel code that can be used today in performance-critical environments, aka Real Life.
Hope this helps!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Why should any one use/learn Perl 6?
by liz (Monsignor) on Jun 11, 2018 at 12:24 UTC | |
by haj (Vicar) on Jun 11, 2018 at 13:47 UTC | |
by liz (Monsignor) on Jun 11, 2018 at 14:03 UTC | |
by haj (Vicar) on Jun 11, 2018 at 18:29 UTC | |
by liz (Monsignor) on Jun 11, 2018 at 20:19 UTC | |
| |
by jeffenstein (Hermit) on Jun 11, 2018 at 19:33 UTC | |
by liz (Monsignor) on Jun 11, 2018 at 20:25 UTC | |
| |
by 1nickt (Canon) on Jun 11, 2018 at 23:22 UTC | |
by liz (Monsignor) on Jun 12, 2018 at 07:43 UTC | |
by hippo (Archbishop) on Jun 12, 2018 at 07:58 UTC | |
by jeffenstein (Hermit) on Jun 12, 2018 at 17:25 UTC | |
by liz (Monsignor) on Jun 12, 2018 at 21:54 UTC | |
| |
by marioroy (Prior) on Jun 13, 2018 at 11:27 UTC | |
by liz (Monsignor) on Jun 13, 2018 at 18:10 UTC | |
| |
Re^3: Why should any one use/learn Perl 6?
by haj (Vicar) on Jun 08, 2018 at 13:14 UTC | |
by 1nickt (Canon) on Jun 08, 2018 at 13:40 UTC |