GithubHelp home page GithubHelp logo

process-control's Introduction

ProcessControl

此包为PHP多进程编程包,主进程开启多子进程,子进程可向主进程通信(基于msg_queue)

安装方法

需要 pcntl 和 posix 扩展

composer require breeze-ev/process-control

实例

  • 多开子进程 1

并发进程控制数为5, 共开10个进程处理业务

$master = new Master(5);
$master->worker(10, function($num, $pid, $ppid){


    echo '第' . $num . '个子进程开启 pid:' . $pid . ' 父进程id:' . $ppid . PHP_EOL;

    sleep(rand(1,3));


    echo '第' . $num . '个子进程退出' . PHP_EOL;

})->start();

print_r($master->result());
  • 多开子进程 2
$master = new Master(5);

for ($i = 0; $i <= 10; $i++)
{
    $master->addWorker(function($num, $pid, $ppid) use ($i){

        echo '第' . $num . '个子进程开启 pid:' . $pid . ' 父进程id:' . $ppid . PHP_EOL;

        sleep(rand(1,3));


        echo '第' . $num . '个子进程退出' . PHP_EOL;

    });
}

$master->start();

$result = $master->result();

print_r($result);
  • 子进程向主进程发送数据
$master = new Master(5);

for ($i = 0; $i <= 10; $i++)
{
    $master->addWorker(function($num, $pid, $ppid) use ($i){

        echo '第' . $num . '个子进程开启 pid:' . $pid . ' 父进程id:' . $ppid . PHP_EOL;

        sleep(rand(1,3));


        echo '第' . $num . '个子进程退出' . PHP_EOL;

        return $i; // 数据返回给主进程
    });
}

$master->start();

$result = $master->result(); // 主进程数据可查看所有子进程返回的 message

print_r($result);

process-control's People

Contributors

breeze-ev avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

yue00er

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.