Skip to content

Instantly share code, notes, and snippets.

@moznion
Created April 22, 2014 10:25
Show Gist options
  • Save moznion/11173299 to your computer and use it in GitHub Desktop.
Save moznion/11173299 to your computer and use it in GitHub Desktop.
#!/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