1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
Coro::LWP - make LWP non-blocking - as much as possible |
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
|
|
use Coro::LWP; # afterwards LWP should not block |
8 |
|
|
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
root |
1.6 |
This module is an L<AnyEvent> user, you need to make sure that you use and |
12 |
|
|
run a supported event loop. |
13 |
|
|
|
14 |
root |
1.1 |
This module tries to make L<LWP|LWP> non-blocking with respect to other |
15 |
|
|
coroutines as much as possible, and with whatever means it takes. |
16 |
|
|
|
17 |
|
|
LWP really tries very hard to be blocking, so this module had to be very |
18 |
|
|
invasive and must be loaded very early to take the proper effect. |
19 |
|
|
|
20 |
|
|
Here is what it currently does (future versions of LWP might require |
21 |
|
|
different tricks): |
22 |
|
|
|
23 |
|
|
=over 4 |
24 |
|
|
|
25 |
|
|
=item It loads Coro::Select, overwriting the perl C<select> builtin I<globally>. |
26 |
|
|
|
27 |
|
|
This is necessary because LWP calls select quite often for timeouts and |
28 |
|
|
who-knows-what. |
29 |
|
|
|
30 |
|
|
Impact: everybody else uses this (slower) version of select, too. It should be quite |
31 |
|
|
compatible to perls builtin select, though. |
32 |
|
|
|
33 |
|
|
=item It overwrites Socket::inet_aton with Coro::Util::inet_aton. |
34 |
|
|
|
35 |
root |
1.2 |
This is necessary because LWP might (and does) try to resolve hostnames |
36 |
|
|
this way. |
37 |
root |
1.1 |
|
38 |
|
|
Impact: likely little, the two functions should be pretty equivalent. |
39 |
|
|
|
40 |
root |
1.2 |
=item It replaces the base class of Net::HTTP, Net::FTP, Net::NNTP. |
41 |
root |
1.1 |
|
42 |
root |
1.2 |
This is necessary because LWP does not always use select to see wether |
43 |
|
|
a filehandle can be read/written without blocking, so the base class |
44 |
|
|
C<IO::Socket::INET> needs to be replaced by C<Coro::Socket>. |
45 |
root |
1.1 |
|
46 |
|
|
Impact: Coro::Socket is not at all compatible to IO::Socket::INET. While |
47 |
|
|
it duplicates some undocumented functionality required by LWP, it does not |
48 |
|
|
have all the methods of IO::Socket::INET and might act quite differently |
49 |
root |
1.2 |
in practise. Also, protocols other than the above mentioned will still block, |
50 |
|
|
at least some of the time. |
51 |
root |
1.1 |
|
52 |
|
|
=back |
53 |
|
|
|
54 |
|
|
All this likely makes other libraries than just LWP not block, but thats |
55 |
|
|
just a side effect you cannot rely on. |
56 |
|
|
|
57 |
|
|
Increases parallelism is not supported by all libraries, some might cache |
58 |
|
|
data globally. |
59 |
|
|
|
60 |
|
|
=cut |
61 |
|
|
|
62 |
|
|
package Coro::LWP; |
63 |
|
|
|
64 |
|
|
use strict; |
65 |
|
|
|
66 |
|
|
use Coro::Select; |
67 |
|
|
use Coro::Util; |
68 |
|
|
use Coro::Socket; |
69 |
|
|
|
70 |
|
|
use Socket; |
71 |
|
|
use IO::Socket::INET; |
72 |
|
|
|
73 |
root |
1.2 |
use Net::HTTP; |
74 |
|
|
use Net::FTP; |
75 |
|
|
use Net::NNTP; |
76 |
|
|
|
77 |
root |
1.5 |
our $VERSION = 4.6; |
78 |
root |
1.3 |
|
79 |
root |
1.1 |
*Socket::inet_aton = \&Coro::Util::inet_aton; |
80 |
|
|
|
81 |
root |
1.2 |
for (@Net::HTTP::ISA, @Net::FTP::ISA, @Net::NTTP::ISA) { |
82 |
|
|
$_ = Coro::LWP::Socket:: if $_ eq IO::Socket::INET::; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
package Coro::LWP::Socket; |
86 |
|
|
|
87 |
|
|
use base Coro::Socket::; |
88 |
|
|
|
89 |
|
|
sub new { |
90 |
|
|
my $self = shift; |
91 |
|
|
|
92 |
|
|
$self->SUPER::new (@_, partial => 1) |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
1; |
96 |
root |
1.1 |
|
97 |
|
|
=head1 AUTHOR |
98 |
|
|
|
99 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
100 |
|
|
http://home.schmorp.de/ |
101 |
|
|
|
102 |
|
|
=cut |
103 |
|
|
|
104 |
|
|
|