首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
监控子进程,挂了之后再重启, 怎么启动了就退出
下面是代码片段。 {{{ /** * 启动 */ public function runAction(){ for($i = 0; $i < $this->maxprocesses; $i++){ $this->createProcess(); } foreach($this->workers as $process){ //子进程也会包含此事件 swoole_event_add($process->pipe, function ($pipe) use($process){ $data = $process->read(); Logs::init(LOGFILE)->addDebug("RECV:{$this->redispid}".$data); if(strpos($data, 'done')!==false){ $this->child--; //$this->createProcess(); Logs::init(LOGFILE)->addDebug("任务执行完成,当前并发{$this->child},当前限制{$this->ctask}"); }else{ Logs::init(LOGFILE)->addDebug("noop"); } }); } swoole_timer_tick(500, function(){ Logs::init(LOGFILE)->addDebug("当前并发:{$this->child}"); while ($this->child < $this->ctask){ $data_pop = $this->rser->rPop($this->queuename); if (!$data_pop){ Logs::init(LOGFILE)->addDebug('没有获取到数据'); break; } $this->child ++; $index = array_rand($this->workers,1); $this->workers[$index]->write($data_pop); Logs::init(LOGFILE)->addDebug("当前并发:{$this->child},work进程:{$index} ,write content: {$data_pop}"); } }); swoole_process::signal(SIGCHLD, function($sig) { //必须为false,非阻塞模式 echo "PID={$sig}\n"; //此方式无法创建进程,创建就退出 while($ret = swoole_process::wait(false)) { echo "PID={$ret['pid']}\n"; unset($this->workers[$ret['pid']]); $this->createProcess(); } }); } /** * 创建 */ protected function createProcess(){ $process = new swoole_process([$this, 'onTask']); $pid = $process->start(); $this->workers[$pid] = $process; Logs::init(LOGFILE)->addDebug("创建进程:{$pid}"); return $this; } }}}
发布于8年前 · 2 次浏览 · 来自
提问
J
Jaws
下面是代码片段。 {{{ /** * 启动 */ public function runAction(){ for($i = 0; $i < $this->maxprocesses; $i++){ $this->createProcess(); } foreach($this->workers as $process){ //子进程也会包含此事件 swoole_event_add($process->pipe, function ($pipe) use($process){ $data = $process->read(); Logs::init(LOGFILE)->addDebug("RECV:{$this->redispid}".$data); if(strpos($data, 'done')!==false){ $this->child--; //$this->createProcess(); Logs::init(LOGFILE)->addDebug("任务执行完成,当前并发{$this->child},当前限制{$this->ctask}"); }else{ Logs::init(LOGFILE)->addDebug("noop"); } }); } swoole_timer_tick(500, function(){ Logs::init(LOGFILE)->addDebug("当前并发:{$this->child}"); while ($this->child < $this->ctask){ $data_pop = $this->rser->rPop($this->queuename); if (!$data_pop){ Logs::init(LOGFILE)->addDebug('没有获取到数据'); break; } $this->child ++; $index = array_rand($this->workers,1); $this->workers[$index]->write($data_pop); Logs::init(LOGFILE)->addDebug("当前并发:{$this->child},work进程:{$index} ,write content: {$data_pop}"); } }); swoole_process::signal(SIGCHLD, function($sig) { //必须为false,非阻塞模式 echo "PID={$sig}\n"; //此方式无法创建进程,创建就退出 while($ret = swoole_process::wait(false)) { echo "PID={$ret['pid']}\n"; unset($this->workers[$ret['pid']]); $this->createProcess(); } }); } /** * 创建 */ protected function createProcess(){ $process = new swoole_process([$this, 'onTask']); $pid = $process->start(); $this->workers[$pid] = $process; Logs::init(LOGFILE)->addDebug("创建进程:{$pid}"); return $this; } }}}
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2018-03-31
新
新用户(手机注册)
检查PHP错误日志,使用strace跟踪子进程。你需要了解子进程是因为什么退出的。
赞
0
回复
2018-04-06
虚
虚竹
{{{ <?php $workers = []; $worker_num = 3;//创建的进程数 for($i=0;$i<$worker_num ; $i++){ $process = new swoole_process('process'); $pid = $process->start(); $workers[$pid] = $process; //$GLOBALS['worker'] = $worker; } foreach($worker
赞
0
回复