swoolephp进程之间通讯案例三

时间:2020-05-20
本文章向大家介绍swoolephp进程之间通讯案例三,主要包括swoolephp进程之间通讯案例三使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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;
        }
    }

原文地址:https://www.cnblogs.com/zh718594493/p/12924668.html