line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
90262
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
84
|
|
2
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
85
|
|
3
|
|
|
|
|
|
|
package LWP::Concurrent; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# ABSTRACT: Runs multiple LWP Client connections in parallel |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
17514
|
use Moo; |
|
2
|
|
|
|
|
49087
|
|
|
2
|
|
|
|
|
14
|
|
9
|
2
|
|
|
2
|
|
6427
|
use Time::HiRes qw(time sleep); |
|
2
|
|
|
|
|
4392
|
|
|
2
|
|
|
|
|
10
|
|
10
|
2
|
|
|
2
|
|
2362
|
use HTTP::Request; |
|
2
|
|
|
|
|
88937
|
|
|
2
|
|
|
|
|
79
|
|
11
|
2
|
|
|
2
|
|
8170
|
use Data::Dumper; |
|
2
|
|
|
|
|
13259
|
|
|
2
|
|
|
|
|
162
|
|
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
2105
|
use HTTP::Async; |
|
2
|
|
|
|
|
170275
|
|
|
2
|
|
|
|
|
70
|
|
14
|
2
|
|
|
2
|
|
23
|
use URI; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
785
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has timeout => ( is => "rw", default => sub { 3.00 } ); |
17
|
|
|
|
|
|
|
has idlesleep => ( is => "rw", default => sub { 0.03 } ); # same as HTTP::Async's poll_interval |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# my $data = get_concurrent( { urls => [$u1, $u2], timeout=>0.99, idlesleep=>0.01 } ); |
20
|
|
|
|
|
|
|
# $data is a hashref where the keys are the urls and the responses are |
21
|
|
|
|
|
|
|
# HTTP::Response objects. |
22
|
|
|
|
|
|
|
# as they come back from HTTP::Async. See text after the __END_ tag below for an example. |
23
|
|
|
|
|
|
|
sub get_concurrent { |
24
|
1
|
|
|
1
|
1
|
20
|
my ($self, %hash) = @_; |
25
|
1
|
|
|
|
|
5
|
my ($urls ) = @hash{ qw( urls ) }; |
26
|
1
|
|
|
|
|
3
|
my @requests = map { HTTP::Request->new( GET => $_ ) } @$urls; |
|
4
|
|
|
|
|
11715
|
|
27
|
1
|
|
|
|
|
90
|
my $ret = $self->operate_concurrently( requests => \@requests ); |
28
|
1
|
|
|
|
|
140
|
return $ret; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub operate_concurrently { |
32
|
1
|
|
|
1
|
1
|
5
|
my ($self, %hash) = @_; |
33
|
1
|
|
|
|
|
3
|
my $requests = $hash{requests}; |
34
|
|
|
|
|
|
|
|
35
|
1
|
|
|
|
|
27
|
my $async = HTTP::Async->new( timeout=>$self->timeout, poll_interval=>$self->idlesleep ); |
36
|
1
|
|
|
|
|
66
|
my $start_t = time(); |
37
|
1
|
|
|
|
|
2
|
my $now; |
38
|
|
|
|
|
|
|
my %to_return; |
39
|
1
|
|
|
|
|
2
|
my $counter = 0; |
40
|
1
|
|
|
|
|
5
|
for my $request (@$requests) { |
41
|
4
|
|
|
|
|
301420
|
$async->add( $request ); # an HTTP::Request object |
42
|
|
|
|
|
|
|
} |
43
|
1
|
|
33
|
|
|
100369
|
while ( ($now = time()) && $now - $start_t < $self->timeout && $async->not_empty ) { |
|
|
|
66
|
|
|
|
|
44
|
7
|
100
|
|
|
|
99280
|
if ( my $response = $async->wait_for_next_response($self->idlesleep) ) { |
45
|
4
|
|
|
|
|
2151
|
my $uri = $response->request()->url(); |
46
|
4
|
|
|
|
|
80
|
$to_return{$counter} = {uri=>$uri, response=>$response}; # uri => the response _object_ |
47
|
4
|
|
|
|
|
46
|
$counter++; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
# else { |
50
|
|
|
|
|
|
|
#sleep( $self->idlesleep ) if $self->idlesleep; # sleep a while if the caller so desires |
51
|
|
|
|
|
|
|
#} |
52
|
|
|
|
|
|
|
} |
53
|
1
|
|
|
|
|
99
|
return \%to_return; # return the data we found; keys are the response numbers, |
54
|
|
|
|
|
|
|
# values are a hash with urls and HTTP::Response objects. |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
1; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=pod |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 NAME |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
LWP::Concurrent -- Provides easy interface to making parallel/concurrent LWP requests |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 SYNOPSIS |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $lwpc = LWP::Concurrent->new() |
69
|
|
|
|
|
|
|
my $responses = $lwpc->get_concurrent( urls=>[ "http://example.com/url1", "http://example.com/url2" ] ); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 DESCRIPTION |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Makes concurrent LWP requests |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 METHODS |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=over 4 |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=item $lwpc = LWP::Concurrent->new( ); # or |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item $lwpc = LWP::Concurrent->new( idlesleep => 0.05, timeout => 0.4 ); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
returns a new LWP::Concurrent object. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item $lwpc->timeout() |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=item $lwpc->idlesleep() |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Gets or sets the timeout or idlesleep params. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=item $results = $lwpc->get_concurrent( urls=> \@urls ) |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
performs a GET on the specified urls, returning a hashref where the keys are the response numbers, |
94
|
|
|
|
|
|
|
and the values are the urls and their responses |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item $results = $lwpc->operate_concurrently( requests => \@request_objects ) |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
performs actions on HTTP servers as specified by the passed request objects, |
99
|
|
|
|
|
|
|
returning a hashref where the keys are the response numbers, |
100
|
|
|
|
|
|
|
and the values are the urls and their responses |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=back |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 TO DO |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
If you want such a section. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 BUGS |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
None |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 COPYRIGHT |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Copyright (c) 2012 Josh Rabinowitz, All Rights Reserved. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 AUTHORS |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Josh Rabinowitz |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |
121
|
|
|
|
|
|
|
|