1 | #!/usr/bin/perl |
1 | #!/usr/bin/perl |
2 | |
|
|
3 | # this hack shows how one can get LWP to be less blocking. |
|
|
4 | |
|
|
5 | { |
|
|
6 | # we replace Socket::inet_aton and CORE::select |
|
|
7 | use Coro::Select; # must come early |
|
|
8 | use Coro::Util; |
|
|
9 | use Coro::Socket; |
|
|
10 | use IO::Socket::INET; |
|
|
11 | use Socket; |
|
|
12 | BEGIN { |
|
|
13 | *Socket::inet_aton = \&Coro::Util::inet_aton; |
|
|
14 | *IO::Socket::INET::new = sub { new Coro::Socket forward_class => @_ }; |
|
|
15 | } |
|
|
16 | } |
|
|
17 | |
2 | |
18 | use Coro; |
3 | use Coro; |
19 | use Coro::Event; |
4 | use Coro::Event; |
|
|
5 | use Coro::LWP; # should be use'd as early as possible |
20 | use LWP::Simple; |
6 | use LWP::Simple; |
21 | |
7 | |
22 | $SIG{PIPE} = 'IGNORE'; |
8 | $SIG{PIPE} = 'IGNORE'; |
23 | |
9 | |
24 | async { |
10 | my @pids; |
25 | print "hi2\n"; |
|
|
26 | get "http://www.google.de/"; |
|
|
27 | print "ho2\n"; |
|
|
28 | }; |
|
|
29 | |
11 | |
30 | async { |
12 | for (1..1) { |
31 | print "hi\n"; |
13 | push @pid, async { |
|
|
14 | print "starting to fetch http://www.google.de/\n"; |
32 | get "http://www.yahoo.com/"; |
15 | get "http://www.google.de/"; |
33 | print "ho\n"; |
16 | print "fetched http://www.google.de/\n"; |
|
|
17 | }; |
34 | |
18 | |
35 | }; |
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"; |
|
|
23 | }; |
|
|
24 | } |
36 | |
25 | |
37 | loop; |
26 | $_->join for @pid; |
38 | |
27 | |