Created
October 11, 2017 16:48
-
-
Save paulofreitas/f9587302b08e85640dbdc65b282313c2 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
<?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