Created
April 22, 2014 10:25
-
-
Save moznion/11173299 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/env perl | |
use strict; | |
use warnings; | |
use feature qw/say/; | |
use Coro; | |
use Coro::AnyEvent; | |
my $var; | |
my $timeout_timer; | |
my $worker; | |
my $timeout_watcher; | |
$timeout_watcher = async { | |
$timeout_timer = AE::timer 2, 0, sub { | |
if (! eval { $worker->safe_cancel }) { | |
eval { $worker->cancel }; | |
warn "error: $@" if $@; | |
} | |
say "Worker canceled"; | |
eval { say $var }; | |
}; | |
}; | |
$worker = async { | |
$var = "Substituted at client!"; | |
Coro::AnyEvent::sleep 5; | |
say "I'm worker"; # <= unreachable! | |
}; | |
$worker->join; | |
if (! eval { $timeout_watcher->safe_cancel }) { | |
$timeout_watcher->cancel; | |
} | |
say "Finished"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment