Create a gist now

Instantly share code, notes, and snippets.

parallel http io::lambda
#!/usr/bin/perl
use strict;
use warnings;
use lib qw(./lib);
use HTTP::Request;
use IO::Lambda qw(:lambda);
use IO::Lambda::HTTP qw(http_request);
use Time::HiRes qw(time);
my @urls = (
"https://www.google.com",
"http://www.windley.com/",
"https://www.bing.com",
"http://www.example.com",
"http://www.wetpaint.com",
"http://www.uh.cu",
);
my $now = time;
sub report
{
my ( $response, $url ) = @_;
if ( ref($response) and ref($response) eq 'HTTP::Response') {
my $url = sprintf "%-25s", $url;
my $len = sprintf "%8s", length $response->content;
my $et = sprintf "%5.3f", time - $now;
print "$url has length $len and loaded in $et ms\n";
$now = time;
} else {
print "$url has problems: $response\n";
}
}
lambda {
context map {
my $url = $_;
lambda {
context (IO::Lambda::HTTP-> new( HTTP::Request-> new( GET => $url )));
tail { report( shift, $url ) }
}
} @urls;
&tails();
}-> wait;
@dk
Owner
dk commented Mar 12, 2012

https://www.bing.com has length 216 and loaded in 0.048 ms
http://www.wetpaint.com has length 4648 and loaded in 0.341 ms
https://www.google.com has length 31651 and loaded in 0.147 ms
http://www.example.com has length 2966 and loaded in 0.172 ms
http://www.windley.com/ has length 83905 and loaded in 0.313 ms
http://www.uh.cu has length 18454 and loaded in 2.415 ms

@oylenshpeegul

Neat! I'd never used IO::Lambda before. But I'm getting a problem...

Use of uninitialized value $IO::Lambda::HTTP::HTTPS::SSL_ERROR in numeric eq (==) at /home/tim/perl5/perlbrew/perls/perl-5.14.2/lib/site_perl/5.14.2/IO/Lambda/HTTP/HTTPS.pm line 26.
Use of uninitialized value $IO::Lambda::HTTP::HTTPS::SSL_ERROR in numeric eq (==) at /home/tim/perl5/perlbrew/perls/perl-5.14.2/lib/site_perl/5.14.2/IO/Lambda/HTTP/HTTPS.pm line 35.
https://www.google.com has problems: ssl
https://www.bing.com      has length      216 and loaded in 1.141 ms
http://www.example.com    has length     2966 and loaded in 0.146 ms
http://www.windley.com/   has length    83896 and loaded in 0.450 ms
http://www.wetpaint.com   has length     1056 and loaded in 0.376 ms
http://www.uh.cu          has length    18518 and loaded in 4.679 ms
@dk
Owner
dk commented Mar 13, 2012
@dk
Owner
dk commented Mar 14, 2012

fixed, if you're interested check https://github.com/dk/IO-Lambda

@oylenshpeegul

Works great! I downloaded it from here with

git clone https://github.com/dk/IO-Lambda.git

and then added

use lib "$ENV{HOME}/IO-Lambda/lib";

to my script. Have you uploaded it to CPAN?

$ cpanm IO::Lambda
IO::Lambda is up to date. (1.22)

Perhaps you did and just forgot to up the version number?

@dk
Owner
dk commented Mar 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment