php实现 master-worker 守护多进程模式的实例代码
php  /  管理员 发布于 7年前   439
具体代码如下所示: 执行命令 kill 65,杀死进程 65 则master_process 进程会再自动开启一个子进程 总结 以上所述是小编给大家介绍的php实现 master-worker 守护多进程模式的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持! 0){ echo "主进程进程 $pid \n"; exit; }else if($pid == 0){ if(-1 === posix_setsid()){ throw new Exception("setsid fail"); } for ($i=0; $i < self::$count; $i++) { static::runWorker(); } @cli_set_process_title("master_process"); }else{ throw new Exception("创建主进程失败"); } } //开启子进程 public static function runWorker(){ unmask(0); $pid = pcntl_fork(); if($pid > 0){ // echo "创建子进程 $pid \n"; }else if($pid == 0){ if(-1 === posix_setsid()){ throw new Exception("setsid fail"); } @cli_set_process_title("worker_process"); while(1){ sleep(1); } }else{ throw new Exception("创建子进程失败"); } } //监控worker进程 public function moniProcess(){ while( $pid = pcntl_wait($status)){ if($pid == -1){ break; }else{ static::runWorker(); } } }}Worker::runAll();
ps -auxUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.0 18200 3076 pts/0 Ss+ 14:05 0:00 bashroot 6 0.0 0.0 18208 3252 pts/1 Ss 14:06 0:00 bashroot 19 0.0 0.0 18204 3248 pts/2 Ss+ 14:11 0:00 bashroot 64 0.0 0.2 348488 8320 ? Ss 15:32 0:00 master_processroot 65 0.0 0.2 348488 8400 ? Ss 15:32 0:00 worker_processroot 66 0.0 0.2 348488 8400 ? Ss 15:32 0:00 worker_processroot 67 0.0 0.0 36640 2804 pts/1 R+ 15:32 0:00 ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.0 18200 3076 pts/0 Ss+ 14:05 0:00 bashroot 6 0.0 0.0 18208 3252 pts/1 Ss 14:06 0:00 bashroot 19 0.0 0.0 18204 3248 pts/2 Ss+ 14:11 0:00 bashroot 64 0.0 0.2 348488 8320 ? Ss 15:32 0:00 master_processroot 66 0.0 0.2 348488 8400 ? Ss 15:32 0:00 worker_processroot 68 0.0 0.1 348488 5796 ? Ss 15:34 0:00 worker_processroot 69 0.0 0.0 36640 2728 pts/1 R+ 15:34 0:00 ps -aux
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号