Skip to content

Instantly share code, notes, and snippets.

@dk
Created March 12, 2012 13:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dk/2021875 to your computer and use it in GitHub Desktop.
Save dk/2021875 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

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
Copy link
Author

dk commented Mar 13, 2012 via email

@dk
Copy link
Author

dk commented Mar 14, 2012

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

@oylenshpeegul
Copy link

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
Copy link
Author

dk commented Mar 15, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment