Skip to content
Newer
Older
100644 52 lines (43 sloc) 1.1 KB
8e536b8 @tadzik Add stupid http_get for async HTTP requests
authored
1 use MuEvent;
2
3 my @urls = <
4 duckduckgo.com
5 cpan.org
6 kosciol-spaghetti.pl
7 perlcabal.org
8 perl6.org
9 >;
10 my $count = @urls.elems;
11 my $starttime;
12
13 sub http_get_eager(:$url!) is export {
14 my $sock = IO::Socket::INET.new(host => $url, port => 80);
15 my $req = "GET / HTTP/1.1\r\n"
16 ~ "Connection: Close\r\n"
17 ~ "Host: $url\r\n"
18 ~ "User-Agent: MuEvent/0.0 Perl6/$*PERL<compiler><ver>\r\n"
19 ~ "\r\n";
20 $sock.send($req);
21 $sock.recv;
22 $sock.close;
23 }
24
25 say "=== BLOCKING FETCHING ===";
26 my $last;
27 $starttime = $last = now;
28 for @urls -> $url {
29 http_get_eager(url => $url);
30 say sprintf "%-25s has loaded in %s", $url, now - $last;
31 $last = now;
32 }
33 say "Finished in {now - $starttime} seconds";
34
35
36 sub handler ($what, $content) {
37 say sprintf "%-25s has loaded in %s", $what, now - $starttime;
38 unless --$count {
39 say "Finished in {now - $starttime} seconds";
40 exit 0;
41 }
42 }
43
44 say "=== NON-BLOCKING FETCHING ===";
45 $starttime = now;
46
47 for @urls -> $url {
48 http_get(url => $url, cb => sub { handler($url, $^content) })
49 }
50
51 MuEvent::run;
Something went wrong with that request. Please try again.