如何用php语言实现实时消息处理的后台守护进程

用php实现后台任务通常有2种方式,一种是cron,也就是定时任务调度了,该方式实现和部署都比较简单,php的代码也能复用,能满足很多场景的业务需求。但是该方式也有个缺点,就是cron最短也只能1分钟调度一次,这对一些要求实时处理的业务还略显不够完美,那能否用php实现可以实时处理的后台任务呢?这就是我们下面要讲的第二种方式,即后台守护进程。

php做后台进程最主要的问题就是僵死问题,代码跑一段时间就会僵死或者崩溃,cron方式由于定时重新调度,所以可以规避僵死问题,但是要是不用cron而是后台守护进程方式运行,那就要必须解决这个问题才行了。

先说明一下,这个解决方案不是我原创的,是我拿来用的,根据具体业务做了修改。具体的原理我就不详细讲了,请看原文的链接

这个解决方案很靠谱,跑了n个月,没有重启过。先把代码贴上,大家参考。

#!/usr/bin/php -q
<?php
ini_set('display_errors',0);
print "Parent : ". getmypid() . "\n";


global $pids;
$pids = Array();


// Daemonize
$pid = pcntl_fork();
if($pid){
// Only the parent will know the PID. Kids aren't self-aware
// Parent says goodbye!
print "\tParent : " . getmypid() . " exiting\n";
exit();
}


print "Child : " . getmypid() . "\n";


// Handle signals so we can exit nicely
declare(ticks = 1);


function sig_handler($signo){
global $pids,$pidFileWritten;
if ($signo == SIGTERM || $signo == SIGHUP || $signo == SIGINT){
// If we are being restarted or killed, quit all children

// Send the same signal to the children which we recieved
foreach($pids as $p){ posix_kill($p,$signo); } 

// Women and Children first (let them exit)
foreach($pids as $p){ pcntl_waitpid($p,$status); }
print "Parent : "
.  getmypid()
. " all my kids should be gone now. Exiting.\n";
exit();
}
else if($signo == SIGUSR1){
print "I currently have " . count($pids) . " children\n";
}
}
// setup signal handlers to actually catch and direct the signals
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP,  "sig_handler");
pcntl_signal(SIGINT, "sig_handler");
pcntl_signal(SIGUSR1, "sig_handler");


// All the daemon setup work is done now. Now do the actual tasks at hand


// The program to launch
$program = "/shell";
$arguments = array("myscript");


while(1){
// In a real world scenario we would do some sort of conditional launch.
// Maybe a condition in a DB is met, or whatever, here we're going to
// cap the number of concurrent grandchildren
if(count($pids) < 6){
$pid=pcntl_fork();
if(!$pid){
pcntl_exec($program,$arguments); // takes an array of arguments
exit();

else {
// We add pids to a global array, so that when we get a kill signal
// we tell the kids to flush and exit.
$pids[] = $pid;
}
}

// Collect any children which have exited on their own. pcntl_waitpid will
// return the PID that exited or 0 or ERROR
// WNOHANG means we won't sit here waiting if there's not a child ready
// for us to reap immediately
// -1 means any child
$dead_and_gone = pcntl_waitpid(-1,$status,WNOHANG);
while($dead_and_gone > 0){
// Remove the gone pid from the array
unset($pids[array_search($dead_and_gone,$pids)]); 

// Look for another one
$dead_and_gone = pcntl_waitpid(-1,$status,WNOHANG);
}

// Sleep for 1 second
sleep(1);
}

不好意思,原文的链接找不到了,最近谷歌上不了,找个东西真是不方便,其他搜索引擎还不够给力啊,这是逼我们自己搞一个啊。加油


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值