note 115022 added to function.pcntl-signal-dispatch
| From: | stefanis | Date: | Tue, 13 May 2014 18:45:49 +0000 |
| Subject: | note 115022 added to function.pcntl-signal-dispatch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199531@lists.php.net to get a copy of this message | ||
Well I misspoke before. It is not enough to process signals outside of the signal handler. They must
be processed outside of the tick handler (explicit or implied). So...
Register a tick handler that calls pcntl_signal_dispatch();
In the signal handler, enqueue your signal;
In the main loop of your script, process your signals;
<?php
declare(ticks=1);
global $sig_queue;
global $use_queue;
$sig_queue = array();
$use_queue = true; // set to false to do it the old way
function tick_handler()
{
pcntl_signal_dispatch();
}
function sig_handler($sig)
{
global $sig_queue;
global $use_queue;
if(isset($use_queue) && $use_queue)
{
$sig_queue[] = $sig;
}
else
{
sig_helper($sig);
}
}
function sig_helper($sig)
{
switch($sig)
{
case SIGHUP:
$pid = pcntl_fork();
if($pid) print("forked $pid\n");
break;
default:
print("unhandled sig: $sig\n");
}
}
pcntl_signal(SIGHUP, "sig_handler");
while(true)
{
if($use_queue) foreach($sig_queue as $idx=>$sig)
{
sig_helper($sig);
unset($sig_queue[$idx]);
}
sleep(1);
}
?>
----
Server IP: 216.194.115.244
Probable Submitter: 216.55.28.6
----
Manual Page -- http://php.net/manual/en/function.pcntl-signal-dispatch.php
Edit -- https://master.php.net/note/edit/115022
Del: integrated -- https://master.php.net/note/delete/115022/integrated
Del: useless -- https://master.php.net/note/delete/115022/useless
Del: bad code -- https://master.php.net/note/delete/115022/bad+code
Del: spam -- https://master.php.net/note/delete/115022/spam
Del: non-english -- https://master.php.net/note/delete/115022/non-english
Del: in docs -- https://master.php.net/note/delete/115022/in+docs
Del: other reasons-- https://master.php.net/note/delete/115022
Reject -- https://master.php.net/note/reject/115022
Search -- https://master.php.net/manage/user-notes.php