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