Permalink
Browse files

This is a simpler version version of eg-22-multiple-http-requests.pl.

This one subclasses HttpClient and adds a pending() attribute to count
the requests currently in play.
  • Loading branch information...
1 parent 3fe11a8 commit 5f75559f7adb71a57bec6add86823e85a2319cbe @rcaputo committed Sep 27, 2012
Showing with 49 additions and 0 deletions.
  1. +49 −0 eg/eg-23-multiple-http-subclass.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+# vim: ts=2 sw=2 noexpandtab
+
+use warnings;
+use strict;
+use lib qw(../lib);
+
+{
+ package ConcurrentHttpClient;
+ use Moose;
+ extends 'HttpClient';
+
+ has pending => (
+ is => 'rw',
+ isa => 'Int',
+ default => 0,
+ );
+
+ after request => sub {
+ my ($self) = @_;
+ $self->pending( $self->pending() + 1 );
+ };
+
+ after internal_http_response => sub {
+ my ($self) = @_;
+ return if $self->pending( $self->pending() - 1 );
+ $self->emit( -name => "empty" );
+ };
+}
+
+### Main usage.
+
+use HTTP::Request;
+
+my $client = ConcurrentHttpClient->new();
+$client->request( HTTP::Request->new( GET => $_ ) ) foreach (
+ 'http://poe.perl.org',
+ 'http://duckduckgo.com/',
+ 'http://metacpan.org/',
+ 'http://perl.org/',
+ 'http://twitter.com/',
+);
+
+while (my $event = $client->next()) {
+ last if $event->_name() eq 'empty';
+ print $event->response()->code(), " = ", $event->request()->uri(), "\n";
+}
+
+exit;

0 comments on commit 5f75559

Please sign in to comment.