PHP.mk документација

pcntl_signal_dispatch

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.pcntl-signal-dispatch.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.pcntl-signal-dispatch.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
pcntl_signal_dispatch

Референца за `function.pcntl-signal-dispatch.php` со подобрена типографија и навигација.

function.pcntl-signal-dispatch.php

pcntl_signal_dispatch

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

pcntl_signal_dispatch(PHP 5 >= 5.3.0, PHP 7, PHP 8)

= NULL

pcntl_signal_dispatch(): bool

На pcntl_signal_dispatch() Ги повикува ракувачите со сигнали за чекачки сигнали pcntl_signal() функцијата ги повикува ракувачите со сигнали инсталирани од

Параметри

Оваа функција нема параметри.

Вратени вредности

Патеката до PHP скриптата што треба да се провери. true на успех или false при неуспех.

Примери

Пример #1 pcntl_signal_dispatch() example

<?php
echo "Installing signal handler...\n";
pcntl_signal(SIGHUP, function($signo) {
echo
"signal handler called\n";
});

echo
"Generating signal SIGHUP to self...\n";
posix_kill(posix_getpid(), SIGHUP);

echo
"Dispatching...\n";
pcntl_signal_dispatch();

echo
"Done\n";

?>

Горниот пример ќе прикаже нешто слично на:

Installing signal handler...
Generating signal SIGHUP to self...
Dispatching...
signal handler called
Done

Види Исто така

Белешки од корисници 4 белешки

Жолт Силаѓи
пред 16 години
If you are running PHP as CLI and as a "daemon" (i.e. in a loop), this function must be called in each loop  to check if new signals are waiting dispatching.
за секој чекачки сигнал.
12 години пред
Note that calling pcntl_signal_dispatch() from within a signal handler invoked by a previous pcntl_signal_dispatch() will not trigger handlers for any new pending signals. This means that if you write a CLI daemon which forks off child processes in response to a signal, then those child processes won't be able to respond to signals. This gave me a headache for a while as pcntl_signal_dispatch() doesn't raise any errors when this occurs. One solution is to set a flag within the signal handler and react upon it (by forking off the needed child processes) elsewhere in the parent process's main loop.
stefanis
пред 11 години
As noted by "me at subsonic dot net" calling pcntl_signal_dispatch() from within a signal handler invoked by a previous pcntl_signal_dispatch() will not trigger handlers for any new pending signals. This seems to be true even if you pcntl_exec() a new PHP processor to execute an entirely different script.

The solution seems to be to explicitly call pcntl_signal_dispatch()inside a ticks_handler() . And use sig_handler(int) as a push function to a queue. Immediately following the call to dispatch in the ticks_handler, pop your queue doing what you would have done in the signal_handler until the queue is empty.
stefanis
пред 11 години
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);
}
?>
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.