自定义进程

最后更新于:2022-04-02 02:21:18

[TOC] 新建 `App/Process/Test.php` ``` namespace App\Process; use EasySwoole\Component\Process\AbstractProcess; class Test extends AbstractProcess{ protected function run($arg){ //当进程启动后,会执行的回调 var_dump($arg); var_dump("processname : ".$this->getProcessName()); var_dump("pid :" . $this->getPid()); $this->addTick(); } public function addTick(): ?int{ return parent::addTick(1*1000, function (){ echo "({$this->getPid()}) hello word\n"; }); } } ``` 在 `mainServerCreate` 方法中 ``` $processConfig = new \EasySwoole\Component\Process\Config(); $processConfig->setProcessName("testProcess"); //可开多个进程 for ($i=0; $i < 3; $i++) { $processConfig->setArg([ 'arg'.$i=>time(), ]); ServerManager::getInstance()->getSwooleServer()->addProcess((new \App\Process\Test($processConfig))->getProcess()); } ``` > 注意,一个进程模型可以被注册N次,也就是创建N个相同类型的进程 输出 ``` [TOC] ... array(1) { ["arg"]=> int(1568816097) } string(25) "processname : testProcess" string(10) "pid :27627" (27625) hello word (27626) hello word (27627) hello word (27626) hello word ```
';