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!
In reply to Re^2: (Easy Perl concurrency with MCE)
by 1nickt
in thread Why should any one use/learn Perl 6?
by skooma
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |