Skip to content

Instantly share code, notes, and snippets.

@kazeburo
Created April 30, 2010 05:00
Show Gist options
  • Save kazeburo/384768 to your computer and use it in GitHub Desktop.
Save kazeburo/384768 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use AnyEvent;
use AnyEvent::Curl;
use Coro;
use Coro::Channel;
use Coro::AnyEvent;
use Time::HiRes qw//;
# worker threads
sub build_channel {
warn "[$$] build channel";
my $channel = Coro::Channel->new();
for my $i (0..29) {
async {
my $ua = AnyEvent::Curl->new;
$ua->start;
while(1) {
my $req = $channel->get();
my $start = Time::HiRes::time;
$ua->add('http://127.0.0.1:5000/', Coro::rouse_cb );
my $res = Coro::rouse_wait;
my $end = Time::HiRes::time - $start;
warn sprintf "[%s][%s] end req: %s ", $i, $req->[0], $end if $end > 1;
$req->[1]->send(1);
}
};
}
return $channel;
}
my $channel;
$channel ||= build_channel;
my $g_cv = AE::cv;
my $start = Time::HiRes::time;
for my $i ( 1..200 ) {
$g_cv->begin;
my $cv = AE::cv;
$channel->put([ $i, $cv ]);
$cv->cb(sub {
my $res = shift->recv;
$g_cv->end;
});
}
$g_cv->recv;
printf "end: %s\n", Time::HiRes::time - $start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment