Skip to content

Instantly share code, notes, and snippets.

@arc
Last active December 21, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arc/6268974 to your computer and use it in GitHub Desktop.
Save arc/6268974 to your computer and use it in GitHub Desktop.
sub call_with_timeout {
my ($timeout, $callback) = @_;
my $timeout_expired;
my $completed = eval {
local $SIG{ALRM} = sub { $timeout_expired = 1; die "alarm\n" };
my $completed = eval {
alarm $timeout;
$callback->();
1;
};
alarm 0;
die $@ if !$completed;
1;
};
die $@ if !$completed && !$timeout_expired;
return !$timeout_expired;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment