Created
November 2, 2019 17:34
-
-
Save renatocron/098bb7f855bd7b049dc05c46062e9941 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use Mojo::Promise; | |
use Mojo::IOLoop::Subprocess; | |
use Mojo::UserAgent; | |
my $subprocess = Mojo::IOLoop::Subprocess->new; | |
$subprocess->run( | |
sub { | |
my $subprocess = shift; | |
my $ua = Mojo::UserAgent->new; | |
my @titles; | |
my %promises; | |
my $id = 0; | |
for (1 .. 10) { | |
$id++; | |
my $request_p = $ua->get_p(('https://perlcon.eu/')); | |
my $mine_id = $id; | |
$promises{$mine_id} = $request_p; | |
$request_p->then( | |
sub { | |
my ($tx) = @_; | |
push @titles, $tx->res->dom->at('title')->text; | |
} | |
)->catch( | |
sub { | |
print STDERR @_; | |
} | |
)->finally( | |
sub { | |
print gmtime . ": finished job $mine_id\n"; | |
delete $promises{$mine_id}; | |
} | |
); | |
Mojo::Promise->race(values %promises)->wait() if scalar keys %promises > 2; | |
} | |
print gmtime . ": waiting for all jobs to end.\n"; | |
Mojo::Promise->all(values %promises)->wait() if scalar keys %promises; | |
print gmtime . ": all done!\n"; | |
return @titles; | |
}, | |
sub { | |
my ($self, @titles) = @_; | |
print join "\n", @titles, ''; | |
} | |
); | |
$subprocess->ioloop->start unless $subprocess->ioloop->is_running; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment