Skip to content

Instantly share code, notes, and snippets.

@Ovid
Created October 18, 2015 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ovid/06ac6e9e0187e84a9589 to your computer and use it in GitHub Desktop.
Save Ovid/06ac6e9e0187e84a9589 to your computer and use it in GitHub Desktop.
Fetching URL titles in parallel (blocks)
use v6;
use LWP::Simple;
use HTML::Parser::XML;
subset PositiveInt of Int where * > 0;
sub MAIN(PositiveInt :$timeout = 60) {
my Str @urls = <
http://www.rakudo.org/
http://www.google.com/
http://www.perlmonks.org/
http://www.reddit.com/
http://www.yahoo.com/
>;
my @promises = @urls.map({
start {
my $document = LWP::Simple.get($_).perl;
my $parser = HTML::Parser::XML.new;
my $xml = $parser.parse($document);
say "$_ - { $xml.getElementsByTagName('title')[0][0] }";
}
});
await Promise.allof(Promise.in($timeout), @promises);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment