首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
swoole_process,子进程自动重启及主进程退出后,子进程退出的试例.
1.子进程异常退出后,自动重启子进程, 2. 主进程异常退出后,子进程在干完当前工作后,自动退出 ```php (new class{ public $mpid=0; public $works=[]; public $max_precess=1; public $new_index=0; public function __construct(){ try { swoole_set_process_name(sprintf('php-ps:%s', 'master')); $this->mpid = posix_getpid(); $this->run(); $this->processWait(); }catch (\Exception $e){ die('ALL ERROR: '.$e->getMessage()); } } public function run(){ for ($i=0; $i < $this->max_precess; $i++) { $this->CreateProcess(); } } public function CreateProcess($index=null){ $process = new swoole_process(function(swoole_process $worker)use($index){ if(is_null($index)){ $index=$this->new_index; $this->new_index++; } swoole_set_process_name(sprintf('php-ps:%s',$index)); for ($j = 0; $j < 16000; $j++) { $this->checkMpid($worker); echo "msg: {$j}\n"; sleep(1); } }, false, false); $pid=$process->start(); $this->works[$index]=$pid; return $pid; } public function checkMpid(&$worker){ if(!swoole_process::kill($this->mpid,0)){ $worker->exit(); // 这句提示,实际是看不到的.需要写到日志中 echo "Master process exited, I [{$worker['pid']}] also quit\n"; } } public function rebootProcess($ret){ $pid=$ret['pid']; $index=array_search($pid, $this->works); if($index!==false){ $index=intval($index); $new_pid=$this->CreateProcess($index); echo "rebootProcess: {$index}={$new_pid} Done\n"; return; } throw new \Exception('rebootProcess Error: no pid'); } public function processWait(){ while(1) { if(count($this->works)){ $ret = swoole_process::wait(); if ($ret) { $this->rebootProcess($ret); } }else{ break; } } } }); ```
发布于8年前 · 3 次浏览 · 来自
提问
蜗牛之梦
1.子进程异常退出后,自动重启子进程, 2. 主进程异常退出后,子进程在干完当前工作后,自动退出 ```php (new class{ public $mpid=0; public $works=[]; public $max_precess=1; public $new_index=0; public function __construct(){ try { swoole_set_process_name(sprintf('php-ps:%s', 'master')); $this->mpid = posix_getpid(); $this->run(); $this->processWait(); }catch (\Exception $e){ die('ALL ERROR: '.$e->getMessage()); } } public function run(){ for ($i=0; $i < $this->max_precess; $i++) { $this->CreateProcess(); } } public function CreateProcess($index=null){ $process = new swoole_process(function(swoole_process $worker)use($index){ if(is_null($index)){ $index=$this->new_index; $this->new_index++; } swoole_set_process_name(sprintf('php-ps:%s',$index)); for ($j = 0; $j < 16000; $j++) { $this->checkMpid($worker); echo "msg: {$j}\n"; sleep(1); } }, false, false); $pid=$process->start(); $this->works[$index]=$pid; return $pid; } public function checkMpid(&$worker){ if(!swoole_process::kill($this->mpid,0)){ $worker->exit(); // 这句提示,实际是看不到的.需要写到日志中 echo "Master process exited, I [{$worker['pid']}] also quit\n"; } } public function rebootProcess($ret){ $pid=$ret['pid']; $index=array_search($pid, $this->works); if($index!==false){ $index=intval($index); $new_pid=$this->CreateProcess($index); echo "rebootProcess: {$index}={$new_pid} Done\n"; return; } throw new \Exception('rebootProcess Error: no pid'); } public function processWait(){ while(1) { if(count($this->works)){ $ret = swoole_process::wait(); if ($ret) { $this->rebootProcess($ret); } }else{ break; } } } }); ```
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2018-05-24
L
Lambda
赞赞赞
赞
0
回复
2018-07-02
P
PZG
xiexie
赞
0
回复