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