<?php
class SwooleProcess{
private $process;
public function __construct(){
// *************************************************************************
// 子进程与父进程基于管道通信
// $this->process = new swoole_process([$this, "onProcess"], false, true);
// $this->process->deamon();
// $this->process->start();
// swoole_event_add($this->process->pipe, [$this, "onReceiveChild"]);
// *************************************************************************
// *************************************************************************
// 子进程与父进程基于消息队列通信
$this->process = new swoole_process([$this,"onProcessForQueue"], false, true);
$this->process->useQueue(1024);
$this->process->start();
$index = 10;
while($index){
$this->process->push($index);
$index -= 1;
}
// *************************************************************************
}
/**
* @return swoole_process
*/
public function onProcessForQueue($worker){
echo "**************************************************\n";
var_dump($worker);
echo "**************************************************\n";
var_dump($this->process);
// swoole_timer_tick(1000, function($timer){
// static $index = 0;
// if($index < 10){
// $this->process->push($this->process->pid + $index);
// $index++;
// }else{
// swoole_timer_clear($timer);
// }
// });
// while(true){
// echo "In child process pop data: ".$this->process->pop()."\n";
// }
}
public function onReceiveChild(){
$buffer = $this->process->read();
echo "Received from child {$buffer}";
}
public function onProcess(){
swoole_timer_tick(1000, function($timer_id){
static $index = 0;
if($index < 10){
$this->process->write("Hellow fater \n");
$index++;
}else{
swoole_timer_clear($timer_id);
}
});
}
}
new SwooleProcess();
swoole_process::signal(SIGCHLD, function(){
while($ret = swoole_process::wait(false)){
echo "PID={$ret['pid']}\n";
}
});
SwooleProcess
最新推荐文章于 2022-03-03 00:21:09 发布