gpt4 book ai didi

PHP sleep 、pcntl_signal 和滴答声

转载 作者:可可西里 更新时间:2023-10-31 23:50:58 26 4
gpt4 key购买 nike

好的,所以我一直在尝试获取 Process Signals使用 PHP 脚本。我遇到了很多我不确定发生了什么的事情,而且我觉得文档没有很好地解释它。

假设我按照 registering signals 上的示例进行操作:

<?php
// tick use required as of PHP 4.3.0
declare(ticks = 1);

// signal handler function
function sig_handler($signo)
{

switch ($signo) {
case SIGTERM:
// handle shutdown tasks
exit;
break;
case SIGHUP:
// handle restart tasks
break;
case SIGUSR1:
echo "Caught SIGUSR1...\n";
break;
default:
// handle all other signals
}

}

echo "Installing signal handler...\n";

// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");
pcntl_signal(SIGUSR1, "sig_handler");

// or use an object, available as of PHP 4.3.0
// pcntl_signal(SIGUSR1, array($obj, "do_something"));

echo"Generating signal SIGTERM to self...\n";

// send SIGUSR1 to current process id
posix_kill(posix_getpid(), SIGUSR1);

echo "Done\n";

?>

但是,在我的 sig_handler 函数中,我在末尾添加了一个 exit(1)echo "Done\n"; 还会被执行吗?我已经尝试过类似的东西,尽管是在类结构中,而且它只是在 我的 sig_handler 被调用之前。

我的类 __construct 调用函数 registerSignals:

private function registerSignals(){
if(function_exists("pcntl_signal")){
//call_user_func([$this, "sigintCleanup"]);
pcntl_signal(SIGINT, [$this, "sigintCleanup"]);
pcntl_signal(SIGTERM, [$this, "sigtermCleanup"]);
pcntl_signal(SIGHUP, [$this, "sighupCleanup"]);
pcntl_signal(SIGUSR1, [$this, "sigusr1Cleanup"]);
}
}

protected function sigintCleanup(){
echo "In SIGINT cleanup\n";
$this->cleanup();
}

protected function sigtermCleanup(){
echo "In SIGTERM cleanup\n";
$this->cleanup();
}

protected function sighupCleanup(){
echo "In SIGHUP cleanup\n";
$this->cleanup();
}

protected function sigusr1Cleanup(){
echo "In SIGUSR1 cleanup\n";
$this->cleanup();
}

protected function cleanup(){
echo "Shutting down\n";
exit(1);
}

然后在我对这个过程的测试中,我做了一些简单的 sleepecho(start 只是一个在之后调用的函数施工和信号注册完成):

function start(){
echo "-- Testing the test script --\n";
sleep(10000);
echo "given how ticks work, this might not be called...\n";
echo "\nAfter sleep... this should not be executed if there was a signal interrupt.\n";
}

所以我在 sleep 期间使用ctl-c。我的结果?

-- Testing the test script --
^Cgiven how ticks work, this might not be called...

After sleep... this should not be executed if there was a signal interrupt.
In SIGINT cleanup
Shutting down

正如您从我的 echo 中看到的那样,我有点想通了 ticks work 负责处理信号之前完成 start 函数。我仍然认为可能是这种情况。事实上,我只是不知道。

我确实在我的程序顶部声明了 ticks:

declare(ticks = 1);

不立即处理信号给我带来了麻烦。例如,如果我的 start 函数中有这段代码(并且我已经测试过了)怎么办?

$count = 0;
while(true){
count++;
echo "count\n";
sleep(5);
}

即使我的cleanup 函数有一个exit,我也永远不会达到那个,因为SIGINT 只是让我从sleep,我继续循环并再次开始 sleeping。

所以这是我的问题:signal 处理在 PHP 中是如何工作的?如何保证收到信号后立即处理?我不只是想要一些代码来解决这个问题,我想要一个解释(或者至少一个好的解释和摘要的链接)。

PHP 版本:5.4.22

操作系统:Ubuntu 12.10

最佳答案

sleep() 会被一些信号打断,它会返回剩余的 sleep 秒数,你可以做这样的事情让它保持 sleep 。

.....
declare(ticks = 1);

$left = sleep(1000)
while ($left > 0) {
$left = sleep($left);
}

关于PHP sleep 、pcntl_signal 和滴答声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23001571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com