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!
The way forward always starts with a minimal test.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.