Skip to content

Instantly share code, notes, and snippets.

@yowcow
Created January 19, 2016 12:38
Show Gist options
  • Save yowcow/c33fe979c767505fb7a8 to your computer and use it in GitHub Desktop.
Save yowcow/c33fe979c767505fb7a8 to your computer and use it in GitHub Desktop.
Parallel HTTP request in Perl 5
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