1 | #!/usr/bin/perl |
1 | #!/usr/bin/perl |
2 | |
2 | |
3 | # this hack shows how one can overwrite |
3 | # this hack shows how one can get LWP to be less blocking. |
4 | # IO::Socket::INET to provide non-blocking sockets by default. |
|
|
5 | # this makes lwp magically non-blocking. I hope. |
|
|
6 | |
4 | |
7 | { |
5 | { |
8 | # this is all the required magic: we replace the constructor. |
6 | # we replace Socket::inet_aton and CORE::select |
|
|
7 | use Coro::Select; # must come early |
|
|
8 | use Coro::Util; |
9 | use Coro::Socket; |
9 | use Coro::Socket; |
10 | use IO::Socket::INET; |
10 | use IO::Socket::INET; |
11 | sub IO::Socket::INET::new { |
11 | use Socket; |
12 | shift; new Coro::Socket @_; |
12 | BEGIN { |
|
|
13 | *Socket::inet_aton = \&Coro::Util::inet_aton; |
|
|
14 | *IO::Socket::INET::new = sub { new Coro::Socket forward_class => @_ }; |
13 | }; |
15 | } |
14 | } |
16 | } |
15 | |
17 | |
16 | use Coro; |
18 | use Coro; |
17 | use Coro::Event; |
19 | use Coro::Event; |
18 | use LWP::Simple; |
20 | use LWP::Simple; |
19 | |
21 | |
|
|
22 | $SIG{PIPE} = 'IGNORE'; |
|
|
23 | |
|
|
24 | async { |
|
|
25 | print "hi2\n"; |
|
|
26 | get "http://www.google.de/"; |
|
|
27 | print "ho2\n"; |
|
|
28 | }; |
|
|
29 | |
20 | async { |
30 | async { |
21 | print "hi\n"; |
31 | print "hi\n"; |
22 | get "http://localhost/"; |
32 | get "http://www.yahoo.com/"; |
23 | print "ho\n"; |
33 | print "ho\n"; |
24 | |
34 | |
25 | }; |
35 | }; |
26 | |
36 | |
27 | async { |
37 | loop; |
28 | print "hi2\n"; |
|
|
29 | get "http://localhost/"; |
|
|
30 | print "ho2\n"; |
|
|
31 | }; |
|
|
32 | |
38 | |
33 | loop; |
|
|