note 70807 added to function.pcntl-signal
| From: | nate at example dot com | Date: | Sun, 29 Oct 2006 12:10:37 +0000 |
| Subject: | note 70807 added to function.pcntl-signal | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119225@lists.php.net to get a copy of this message | ||
If you have a script that needs certain sections to not be interrupted by a signal (especially
SIGTERM or SIGINT), but want to make your script ready to process that signal ASAP, there's
only one way to do it. Flag the script as having received the signal, and wait for your script to
say its ready to process it.
Here's a sample script:
<?
$allow_exit = true; // are we permitting exit?
$force_exit = false; // do we need to exit?
declare(ticks = 1);
register_tick_function('check_exit');
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
function sig_handler () {
global $allow_exit, $force_exit;
if ($allow_exit)
exit;
else
$force_exit = true;
}
function check_exit () {
global $allow_exit, $force_exit;
if ($force_exit && $allow_exit)
exit;
}
$allow_exit = false;
$i = 0;
while (++$i) {
echo "still going (${i})\n";
if ($i == 10)
$allow_exit = true;
sleep(2);
}
?>
You set $allow_exit to true at all times when it is perfectly acceptable that your script could exit
without warning. In sections where you really need the script to continue running, you set
$allow_exit to false. Any signals received while $allow_exit is false will not take effect until you
set $allow_exit to true.
<?
$allow_exit = true;
// unimportant stuff here. exiting will not harm anything
$allow_exit = false;
// really important stuff not to be interrupted
$allow_exit = true;
// more unimportant stuff. if signal was received during
// important processing above, script will exit here
?>
----
Server IP: 216.235.15.211
Probable Submitter: 68.148.104.116
----
Manual Page -- http://www.php.net/manual/en/function.pcntl-signal.php
Edit -- https://master.php.net/note/edit/70807
Del: integrated -- https://master.php.net/note/delete/70807/integrated
Del: useless -- https://master.php.net/note/delete/70807/useless
Del: bad code -- https://master.php.net/note/delete/70807/bad+code
Del: spam -- https://master.php.net/note/delete/70807/spam
Del: non-english -- https://master.php.net/note/delete/70807/non-english
Del: in docs -- https://master.php.net/note/delete/70807/in+docs
Del: other reasons-- https://master.php.net/note/delete/70807
Reject -- https://master.php.net/note/reject/70807
Search -- https://master.php.net/manage/user-notes.php