Greetings, perlygapes
The shared variable is constructed using OO, not via the TIE interface. Therefore, assigning to 0 overwrites the variable. Incrementing is possible via $process->incr. Another way is MCE->chunk_id (2nd example). MCE::Mutex is helpful for one worker to access a resource, blocking others (3rd example).
$process->incr()
#!/usr/bin/perl
use strict; use warnings; use 5.010;
use Data::Dumper; use HTTP::Tiny;
use Time::HiRes 'gettimeofday', 'tv_interval';
use MCE; use MCE::Shared;
my $ua = HTTP::Tiny->new( timeout => 10 );
my @urls = qw< gap.com amazon.com ebay.com lego.com wunderground.com
imdb.com underarmour.com disney.com espn.com dailymail.com >;
my $report = MCE::Shared->hash;
my $process = MCE::Shared->scalar(0);
MCE->new( max_workers => 6 )->foreach( \@urls, sub {
my $start = [gettimeofday];
say $process->incr()."->GETting https://".$_;
$ua->get('https://' . $_);
$report->set( $_, tv_interval($start, [gettimeofday]) );
});
say Dumper $report->export;
MCE->chunk_id()
#!/usr/bin/perl
use strict; use warnings; use 5.010;
use Data::Dumper; use HTTP::Tiny;
use Time::HiRes 'gettimeofday', 'tv_interval';
use MCE; use MCE::Shared;
my $ua = HTTP::Tiny->new( timeout => 10 );
my @urls = qw< gap.com amazon.com ebay.com lego.com wunderground.com
imdb.com underarmour.com disney.com espn.com dailymail.com >;
my $report = MCE::Shared->hash;
MCE->new( max_workers => 6 )->foreach( \@urls, sub {
my $start = [gettimeofday];
say MCE->chunk_id()."->GETting https://".$_;
$ua->get('https://' . $_);
$report->set( $_, tv_interval($start, [gettimeofday]) );
});
say Dumper $report->export;
Assessing 3rd DB -- one worker
use strict; use warnings; use 5.010;
use Data::Dumper; use HTTP::Tiny;
use Time::HiRes 'gettimeofday', 'tv_interval';
use MCE; use MCE::Shared; use MCE::Mutex;
my $ua = HTTP::Tiny->new( timeout => 10 );
my @urls = qw< gap.com amazon.com ebay.com lego.com wunderground.com
imdb.com underarmour.com disney.com espn.com dailymail.com >;
my $report = MCE::Shared->hash;
my $mutex = MCE::Mutex->new();
MCE->new( max_workers => 6 )->foreach( \@urls, sub {
my $start = [gettimeofday];
say MCE->chunk_id()."->GETting https://".$_;
$ua->get('https://' . $_);
$report->set( $_, tv_interval($start, [gettimeofday]) );
# access 3rd DB, one worker
$mutex->lock;
# update ...
$mutex->unlock;
# ditto
$mutex->enter( sub {
# update ...
});
});
say Dumper $report->export;
Regards, Mario
-
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.