GithubHelp home page GithubHelp logo

eilenc / swoole-co-pool Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yurunsoft/swoole-co-pool

0.0 1.0 0.0 74 KB

Swoole 协程工作池——来自宇润 PHP 全家桶

Home Page: https://gitee.com/yurunsoft/swoole-co-pool

License: MIT License

PHP 100.00%

swoole-co-pool's Introduction

swoole-co-pool

Latest Version License

介绍

本项目为 Swoole 协程工作池,它封装了一些实用的 Swoole 协程操作,使用起来十分一把梭。

  • 协程工作池

用于需要大量协程任务的场景,它可以限定你的同时工作协程数量,并且减少协程频繁创建销毁的损耗。

  • 协程批量执行器

用于同时执行多个协程,并且能够获取到他们所有的返回值。

宇润PHP全家桶群:17916227 点击加群,如有问题会有人解答和修复。

程序员日常划水群:74401592 点击加群

安装

在您的composer.json中加入配置:

{
    "require": {
        "yurunsoft/swoole-co-pool": "^1.3.0"
    }
}

然后执行composer update命令。

使用

协程工作池

use Yurun\Swoole\CoPool\CoPool;
use Yurun\Swoole\CoPool\Interfaces\ICoTask;
use Yurun\Swoole\CoPool\Interfaces\ITaskParam;

$coCount = 10; // 同时工作协程数
$queueLength = 1024; // 队列长度
$pool = new CoPool($coCount, $queueLength,
    // 定义任务匿名类,当然你也可以定义成普通类,传入完整类名
    new class implements ICoTask
    {
        /**
         * 执行任务
         *
         * @param ITaskParam $param
         * @return mixed
         */
        public function run(ITaskParam $param)
        {
            // 执行任务
            return true; // 返回任务执行结果,非必须
        }

    }
);
$pool->run();

$data = 1; // 可以传递任何参数

// 增加任务,并挂起协程等待返回任务执行结果
$result = $pool->addTask($data);

// 增加任务,异步回调
$result = $pool->addTaskAsync($data, function(ITaskParam $param, $data){
    // 异步回调
});

批量执行协程

每个方法都在单独的协程中被执行,然后可以统一获取到结果。

$batch = new CoBatch([
    function(){
        return 'imi';
    },
    'a' =>  function(){
        return 'niu';
    },
    'b' =>  function(){
        return 'bi';
    },
]);
$results = $batch->exec();
// $timeout = -1; // 支持超时
// $limit = -1; // 限制同时工作协程数量
// $results = $batch->exec($timeout, $limit);
var_dump($results);
// $results 值为:
// [
//     'imi',
//     'a' =>  'niu',
//     'b' =>  'bi',
// ]

快捷函数:

use function Yurun\Swoole\Coroutine\batch;
batch([
    function(){
        return 'imi';
    },
    'a' =>  function(){
        return 'niu';
    },
    'b' =>  function(){
        return 'bi';
    },
]);
// batch($callables, $timeout, $limit);

执行单个协程并等待返回值

use function Yurun\Swoole\Coroutine\goWait;
$result = goWait(function(){
    \Swoole\Coroutine::sleep(1);
    return 'wait result';
});
echo $result; // wait result

通道容器

use Yurun\Swoole\CoPool\ChannelContainer;

go(function(){
    $channelContainer = new ChannelContainer;

    $id = 'abc';
    $data = [
        'time'  =>  time(),
    ];

    go(function() use($id, $data, $channelContainer){
        echo 'Wait 3 seconds...', PHP_EOL;
        \Swoole\Coroutine::sleep(3);
        $channelContainer->push($id, $data);
    });
    var_dump($channelContainer->pop($id));

});

代码示例

详见 example 目录

捐赠

开源不求盈利,多少都是心意,生活不易,随缘随缘……

swoole-co-pool's People

Contributors

yurunsoft avatar

Watchers

James Cloos avatar

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.