Skip to content

Instantly share code, notes, and snippets.

@paulofreitas
Created October 11, 2017 16:48
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 paulofreitas/f9587302b08e85640dbdc65b282313c2 to your computer and use it in GitHub Desktop.
Save paulofreitas/f9587302b08e85640dbdc65b282313c2 to your computer and use it in GitHub Desktop.
<?php
class TimeoutException extends RuntimeException
{
//
}
function wait_until(callable $callback, $seconds)
{
declare(ticks = 1);
pcntl_signal(SIGALRM, function () use ($seconds) {
throw new TimeoutException("Callback exceeded the $seconds seconds timeout");
}, true);
pcntl_alarm($seconds);
return $callback();
}
try {
wait_until(function () {
sleep(5);
print 'It works!';
}, 3);
} catch (TimeoutException $exception) {
print 'Timeout ¯\_(ツ)_/¯';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment