Skip to content

Instantly share code, notes, and snippets.

@halaei
Forked from jsjohnst/pcntl_alarm.php
Created May 3, 2016 17:51
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 halaei/b6a480e67d7c9306c7e48405902b0aa9 to your computer and use it in GitHub Desktop.
Save halaei/b6a480e67d7c9306c7e48405902b0aa9 to your computer and use it in GitHub Desktop.
<?php
declare(ticks = 1);
class TimeoutException extends Exception {};
function signal_handler($signal) {
throw new TimeoutException();
}
pcntl_signal(SIGALRM, "signal_handler", true);
pcntl_alarm(1);
try {
// simulate long executing block
while(1) sleep(30);
} catch(TimeoutException $e) {
echo "Timeout exceeded";
}
echo "done";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment