Skip to content

Instantly share code, notes, and snippets.

@beppu
Created July 13, 2009 11:36
Show Gist options
  • Select an option

  • Save beppu/146056 to your computer and use it in GitHub Desktop.

Select an option

Save beppu/146056 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Coro;
use Coro::AnyEvent;
use Coro::Channel;
use Coro::Signal;
use Coro::Timer;
use AnyEvent;
my $channel = Coro::Channel->new;
# Change $delay to 3 to see the timeout.
my $delay = 1;
my $timeout = 2;
my $producer = async {
my $t = AnyEvent->timer(after => $delay, cb => sub { $channel->put("hi") });
};
my $timed_out;
my $activity = Coro::Signal->new;
my @consumers = (
async {
warn $channel->get;
$activity->broadcast;
},
async {
my $t = Coro::Timer::timeout $timeout;
while (not $t) {
Coro::schedule;
}
warn "timeout!";
$timed_out = 1;
$activity->broadcast;
},
);
$activity->wait;
$_->cancel for (@consumers);
if ($timed_out) {
# do your clean-up here.
warn "too slow...";
} else {
# ...
warn "fast enough";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment