Asynchronous Signal Handling

The executor of PHP’s runtime was extended with safe time-out and interrupt handling capabilities. This allows for callbacks to be called on each loop iteration, userland function entry, or internal function exit and does not require the clumsy, old-fashioned ticks feature.

Asynchronous signal handling is turned off by default. To enable it, simply call pcntl_async_signals(1). You can then use signal handling functions such as pcntl_signal() and pcntl_alarm(), for instance, as usual:

    function () {
        throw new Exception;



In the example shown above, we register an anonymous function as a callback for the SIGALRM event using the pcntl_signal() function. We then use the pcntl_alarm() function to instruct PHP to trigger an SIGALRM event after one second. We then sleep() for two seconds but are interrupted by an exception after one second because of the alarm:

Fatal error: Uncaught Exception in example.php:5
Stack trace:
#0 example.php(13): {closure}(14, Array)
#1 {main}
  thrown in example.php on line 5

Please note that such an alarm cannot be triggered while the PHP runtime is performing an I/O operation or is executing library code that is used by an extension. An alarm such as the one shown in the previous example will then be triggered as soon as the PHP runtime finished an I/O operation, for instance.