Created
January 19, 2016 12:38
-
-
Save yowcow/c33fe979c767505fb7a8 to your computer and use it in GitHub Desktop.
Parallel HTTP request in Perl 5
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
use common::sense; | |
use LWP::UserAgent; | |
use HTTP::Request::Common; | |
use POSIX qw(:sys_wait_h); | |
use Test::More; | |
my @urls = qw( | |
http://hoge.fuga/ | |
http://fuga.hoge/ | |
http://fooo.baar/ | |
); | |
pipe my $read, my $write; | |
for my $url (@urls) { | |
my $pid = fork // die $!; | |
if ($pid == 0) { | |
close $read; | |
say "Making request to $url..."; | |
my $ua = LWP::UserAgent->new; | |
my $res = $ua->request(GET $url); | |
say "Got response from $url with status code: @{[ $res->code ]}"; | |
say {$write} "URL: $url (Code: @{[ $res->code ]})"; | |
exit; | |
} | |
} | |
close $write; | |
my @result = <$read>; | |
waitpid -1, WNOHANG; | |
is @result, 3; | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment