-
-
Save Ovid/06ac6e9e0187e84a9589 to your computer and use it in GitHub Desktop.
Fetching URL titles in parallel (blocks)
This file contains 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
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