Created
July 13, 2009 11:36
-
-
Save beppu/146056 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/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