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