pcntl_alarm

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

pcntl_alarmЗадать таймер доставки сигнала SIGALRM

Описание

pcntl_alarm(int $seconds): int

Создаёт таймер, который отправит сигнал SIGALRM процессу через заданное количество секунд. Любой вызов функции pcntl_alarm() отменит любой ранее заданный таймер.

Список параметров

seconds

Число секунд ожидания. Если в аргумент seconds передан ноль, то новый таймер создан не будет.

Возвращаемые значения

Возвращает время в секундах, которое оставалось любому предыдущему таймеру для доставки сигнала, или вернёт 0, если ранее таймер не был задан.

add a note add a note

User Contributed Notes 2 notes

up
9
kuba at valentine dot dev
3 years ago
This is that universal timeout functionality you dreamed about and always wanted to have and guess what - it's as reliable as it gets, it's basically bulletproof. It can interrupt absolutely anything you throw at it and more, you name it - socket_connect(), socket_read(), fread(), infinite while() loops, sleep(), semaphores - seriously, any blocking operation. You can specify your own handler and just get over anything that normally would make your code unresponsive.

<?php
/**
* Because we shouldn't handle asynchronous
* events in synchronous manner.
*/
pcntl_async_signals(TRUE);

/**
* Some flag we can change to know for sure
* that our operation timed out.
*/
$timed_out = FALSE;

/**
* Register SIGALRM signal handler to avoid
* getting our process killed when signal arrives.
*/
pcntl_signal(SIGALRM, function($signal) use ($timed_out) {
 
$timed_out = TRUE;
});

/**
* Now we set our timeout for 2 seconds, but it's not set in stone
* we can call pcntl_alarm() anytime to extend or to turn if off.
*/
pcntl_alarm(2);

/**
* Here we do something with unpredictable outcome that could
* possibly block our program for a very long time.
* I like sockets as an example, but it can be anything.
*/
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket, 'irc.ircnet.com', 6667);

/**
* If our blocking operation didn't timed out then
* timer is still ticking, we should turn it off ASAP.
*/
$timed_out || pcntl_alarm(0);

/**
* And now we do whatever we want to do.
*/
$status = $connection ? 'Connected.' : ($timed_out ? 'Timed out.' : socket_strerror(socket_last_error($socket)));
echo
'STATUS: '. $status . PHP_EOL;
up
-2
Gao,Shengwei
6 years ago
Use pcntl_signal_dispatch() to catch the signal, don't use declare(ticks=1) because it is ineffcient

<?php
pcntl_signal
(SIGALRM, function () {
    echo
'Received an alarm signal !' . PHP_EOL;
},
false);

pcntl_alarm(5);

while (
true) {
   
pcntl_signal_dispatch();
   
sleep(1);
}
To Top