swoole进程之间通讯案例【三】

1.进程1写取数据,主进程读数据写数据,进程2读取数据

    $child1 = new swoole_process("children",false,true);
    //进程1写
    function children($process){
        static $index=0;
        while(true){
            $index++;
            if($index == 20){
                $process->write("exit");
                $process->exit();//结束子进程
            }
            $process->write($index);
            sleep(1);
        }
    }
    $child1->start();

    //进程2读
    $child2 = new swoole_process("children2",false,true);
    function children2($process){
        while(true){
            $index = $process->read();
            $data = "从子进程2里面读取数据".$index.PHP_EOL;
            file_put_contents("/tmp/1.log", $data,FILE_APPEND);
            if($index == "exit"){
                $process->exit();
            }
            sleep(1);
        }
    }
    $child2->start();
    //主进程写数据
    while(true){
        $index = $child1->read();
        $child2->write($index);
        sleep(1);
        if($index == "exit"){
            exit;
        }
    }

 

posted on 2020-05-20 17:01  孤灯引路人  阅读(384)  评论(0编辑  收藏  举报

导航