Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stupid http_get for async HTTP requests
- Loading branch information
Tadeusz Sośnierz
committed
Nov 26, 2012
1 parent
4d26dab
commit 8e536b8
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,51 @@ | |||
use MuEvent; | |||
|
|||
my @urls = < | |||
duckduckgo.com | |||
cpan.org | |||
kosciol-spaghetti.pl | |||
perlcabal.org | |||
perl6.org | |||
>; | |||
my $count = @urls.elems; | |||
my $starttime; | |||
|
|||
sub http_get_eager(:$url!) is export { | |||
my $sock = IO::Socket::INET.new(host => $url, port => 80); | |||
my $req = "GET / HTTP/1.1\r\n" | |||
~ "Connection: Close\r\n" | |||
~ "Host: $url\r\n" | |||
~ "User-Agent: MuEvent/0.0 Perl6/$*PERL<compiler><ver>\r\n" | |||
~ "\r\n"; | |||
$sock.send($req); | |||
$sock.recv; | |||
$sock.close; | |||
} | |||
|
|||
say "=== BLOCKING FETCHING ==="; | |||
my $last; | |||
$starttime = $last = now; | |||
for @urls -> $url { | |||
http_get_eager(url => $url); | |||
say sprintf "%-25s has loaded in %s", $url, now - $last; | |||
$last = now; | |||
} | |||
say "Finished in {now - $starttime} seconds"; | |||
|
|||
|
|||
sub handler ($what, $content) { | |||
say sprintf "%-25s has loaded in %s", $what, now - $starttime; | |||
unless --$count { | |||
say "Finished in {now - $starttime} seconds"; | |||
exit 0; | |||
} | |||
} | |||
|
|||
say "=== NON-BLOCKING FETCHING ==="; | |||
$starttime = now; | |||
|
|||
for @urls -> $url { | |||
http_get(url => $url, cb => sub { handler($url, $^content) }) | |||
} | |||
|
|||
MuEvent::run; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters