SwooleProcess

<?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";
    }
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值