note 128078 added to function.pcntl-alarm
| From: | kuba at valentine dot dev | Date: | Tue, 03 Jan 2023 05:32:20 +0000 |
| Subject: | note 128078 added to function.pcntl-alarm | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220034@lists.php.net to get a copy of this message | ||
Keep in mind that you can easily and selectively prevent signal types of your choosing (including
SIGALRM) from interrupting blocking operations you don't want interrupted. Use
pcntl_sigprocmask() to shield any part of your code, whenever needed and without affecting expected
signal behavior for the rest of it.
Here is simple example of shielding one blocking sleep operation from being interrupted while
allowing it to happen to another - right next to each other.
<?php
$alarm = 2;
$sleep = 10;
$stamp = time();
pcntl_async_signals(TRUE);
/**
* Alarm signal handler.
*/
pcntl_signal(SIGALRM, function(int $sig) use ($alarm, &$stamp) {
$late = (($now = time()) - $stamp) - $alarm;
$stamp = $now;
echo '* ALARM SIGNAL HANDLER * Fired '. ($late ? ($late .' seconds late') :
'on schedule') .' *'. PHP_EOL;
pcntl_alarm($alarm);
});
/**
* Sleeping function.
*/
function get_some_sleep(int $duration, string $info) {
$start = time();
echo PHP_EOL . $duration .' seconds sleep - '. $info . PHP_EOL;
sleep($duration);
$early = $duration - (time() - $start);
echo 'Sleep was '. ($early ? ('interrupted. Woke up '. $early .' seconds
early.') : 'uninterrupted.') . PHP_EOL;
}
/**
* Here we schedule first alarm.
*/
pcntl_alarm($alarm);
while (TRUE) {
/**
* This sleep can and will be interrupted by alarm signal.
*/
get_some_sleep($sleep, 'without a shield');
/**
* Now we set our shield. From now on blocked signals will be ignored.
* Each blocked signal type that fires while shield is up is put on hold.
*/
pcntl_sigprocmask(SIG_BLOCK, [SIGALRM]);
get_some_sleep($sleep, 'protected by sigprocmask()');
/**
* Now we take off our shield. If any blocked signal fired while shield
* was up now it's handler will be executed immediately, single time.
* This is very useful behaviour because we protect what needs protecting but also
* don't miss out on anything - without being flooded thanks to "once per type"
queue.
*/
pcntl_sigprocmask(SIG_UNBLOCK, [SIGALRM]);
}
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.158.213 (proxied: 31.30.164.136)
----
Manual Page -- https://php.net/manual/en/function.pcntl-alarm.php
Edit -- https://main.php.net/note/edit/128078
Del: integrated -- https://main.php.net/note/delete/128078/integrated
Del: useless -- https://main.php.net/note/delete/128078/useless
Del: bad code -- https://main.php.net/note/delete/128078/bad+code
Del: spam -- https://main.php.net/note/delete/128078/spam
Del: non-english -- https://main.php.net/note/delete/128078/non-english
Del: in docs -- https://main.php.net/note/delete/128078/in+docs
Del: other reasons-- https://main.php.net/note/delete/128078
Reject -- https://main.php.net/note/reject/128078
Search -- https://main.php.net/manage/user-notes.php