请问大佬,我想用workerman来做redis stream的消费端,我试了这么写,好像发送信号,响应不了啊
<?php
use Workerman\Events\EventInterface;
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker();
$worker->count = 1;
$worker->onWorkerStart = function ($worker) {
echo 'Pid is ' . posix_getpid() . "\n";
// 当进程收到SIGALRM信号时,打印输出一些信息
Worker::$globalEvent->add(SIGALRM, EventInterface::EV_SIGNAL, function () {
//??? 打印不了啊
echo "Get signal SIGALRM\n";
});
while (true) {
//这里做redis 阻塞读取 消息
echo "123\n";
sleep(3);
}
};
// 运行worker
Worker::runAll();
1、信号无法响应原因是:
while死循环剥夺了workerman的控制权,换句话也就是导致event.loop()循环永远都执行不到;
2、建议将while死循环改成定时器;
定时器实现
改成定时器处理消息的速度会受到定时器的影响,看来无法用信号解决,还是另寻他法好了。