1 | #!/usr/bin/perl |
1 | #!/usr/bin/perl |
2 | |
2 | |
3 | # this hack shows how one can overwrite |
3 | use Coro::LWP; # should be use'd as early as possible |
4 | # IO::Socket::INET to provide non-blocking sockets by default. |
|
|
5 | # this makes lwp magically non-blocking. I hope. |
|
|
6 | |
4 | |
7 | { |
5 | use Coro; |
8 | # this is all the required magic: we replace the constructor. |
6 | use Coro::AnyEvent; |
9 | use Coro::Socket; |
7 | use LWP::Simple; |
10 | use IO::Socket::INET; |
8 | |
11 | sub IO::Socket::INET::new { |
9 | $SIG{PIPE} = 'IGNORE'; |
12 | shift; new Coro::Socket @_; |
10 | |
|
|
11 | my @pids; |
|
|
12 | |
|
|
13 | for (1..1) { |
|
|
14 | push @pid, async { |
|
|
15 | print "starting to fetch http://www.google.de/\n"; |
|
|
16 | get "http://www.google.de/"; |
|
|
17 | print "fetched http://www.google.de/\n"; |
|
|
18 | }; |
|
|
19 | |
|
|
20 | push @pid, async { |
|
|
21 | print "starting to fetch http://www.yahoo.com/\n"; |
|
|
22 | get "http://www.yahoo.com/"; |
|
|
23 | print "fetched http://www.yahoo.com/\n"; |
13 | }; |
24 | }; |
14 | } |
25 | } |
15 | |
26 | |
16 | use Coro; |
27 | $_->join for @pid; |
17 | use Coro::Event; |
|
|
18 | use LWP::Simple; |
|
|
19 | |
28 | |
20 | async { |
|
|
21 | print "hi\n"; |
|
|
22 | get "http://localhost/"; |
|
|
23 | print "ho\n"; |
|
|
24 | |
|
|
25 | }; |
|
|
26 | |
|
|
27 | async { |
|
|
28 | print "hi2\n"; |
|
|
29 | get "http://localhost/"; |
|
|
30 | print "ho2\n"; |
|
|
31 | }; |
|
|
32 | |
|
|
33 | loop; |
|
|