Cluster for CLI worker
Pull request Compare This branch is even with hfcorriez:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
lib/Pagon
.gitignore
README.md
README_en.md
composer.json

README.md

其他语言:English

PHP-Cluster

Cluster用来在CLI模式下管理工作进程,支持中间件方便扩展

安装

添加 "pagon/cluster": "*"composer.json.

composer.phar install

使用

简单方式

$cluster = new Cluster();

if ($cluster->isMaster()) {
    // 生成子进程
    $cluster->fork(__FILE__);

    // 一直运行
    $cluster->forever();
} else {
    // 处理工作
}

高级用法

$cluster = new Cluster();

// 设置最多可运行的worker数量
$cluster->setMaxChildren(3);

if ($cluster->isMaster()) {
    // 启用保存PID files中间件
    $cluster->add('PidFiles', './pids');

    // 启用自动重启中间件
    $cluster->add('AutoRestart');

    // 当生成进程时
    $cluster->on('fork', function($worker){
        $worker->send();
    });

    // 当退出时
    $cluster->on('exit', function($worker, $code){
        // $worker 退出
    });

    // 当收到消息时
    $cluster->on('message', function($worker, $code){
        // $worker 发了一个消息
    });

    // Fork一个worker出来
    $worker = $cluster->fork(__FILE__);

    // 运行起来
    $cluster->forever();
} else {
    // 处理工作
}

例子

License

(The MIT License)

Copyright (c) 2012 hfcorriez <hfcorriez@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.