承接 carno-php/coroutine 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

carno-php/coroutine

Composer 安装命令:

composer require carno-php/coroutine

包简介

README 文档

README

Installation

composer require carno-php/coroutine

Concepts

Cooperative multitasking using coroutines (in PHP!)

yield values

Generator

When yield a function and it's returned a generator (yield in function), job will make a roll and have a chance to execute other jobs, some functions like defer depends on this feature

Promised

Yield a Promise will make coroutine sleep when pending, and wakeup when promise resolves or rejects, general used with async IO

Syscall

Syscall can make user in coroutine to get job's properties or interact with job, such as get job's id or ctx

Functions

namespace in Carno\Coroutine

go

New coroutine without results but throws exception if error

function go(mixed $program, Context $ctx = null) : void

usages

// use closure
go(function () {
    yield 'something';
    return 'done';
});

// with exceptions
try {
    go(function () {
        throw new Exception('test');
    });
} catch (Throwable $e) {
    echo 'got exception :: ', $e->getMessage(), PHP_EOL;
}

co

Similar with go but returns closure and you can execute it later

function co(mixed $program, Context $ctx = null) : Closure

usages

$func = co(function (string $input) {
    yield 'something';
    echo $input;
});
$func('vars');

async

Base of commands go and co, it returns a promise that sync with coroutine's result

Returned promise will resolving when coroutine finish or rejecting when coroutine throws exception

function async(mixed $program, Context $ctx = null, mixed ...$args) : Promised

usages

async(function () {
    yield msleep(500);
})->then(function () {
    echo 'you will see me after 500ms', PHP_EOL;
});

await

A delegate for works with async IO event, supports timeout

function await(Closure $dial, Closure $awake,
    int $timeout = 60000, string $error = TimeoutException::class, string $message = '') : Promised

usages

$async = function ($callback) {
    $callback(111, 222);
};
yield await(function (Closure $awake) use ($async) {
    $async($awake);
}, function (int $a, int $b) {
    echo 'a = ', $a, ' b = ', $b, PHP_EOL;
});

timeout

Create a promise that throws exception after N milliseconds if no one rejects it in time, useful with race

function timeout(int $ms, string $ec = TimeoutException::class, string $em = '') : Promised

usages

yield race(timeout(rand(5, 10)), timeout(rand(5, 10), CustomException::class, 'custom message'));

msleep

Create a promise that resolves after N milliseconds

function msleep(int $ms, Closure $do = null) : Promised

usages

// make sleep
yield msleep(200);
// do something when wake
echo 'you will see hello -> ', yield msleep(300, function () {
    return 'hello';
}), PHP_EOL;

ctx

Get job's ctx in coroutine

usages

go(function () {
    echo 'hello ', (yield ctx())->get('hello'), PHP_EOL;
}, (new Context)->set('hello', 'world'));

defer

A defer statement defers the execution of a function until the surrounding function returns.

usages

yield (function () {
    yield defer(function ($stage) {
        // $stage is returned value or last yield value or throwable if exception
        echo 'in defer', PHP_EOL;
    });
    echo 'in end', PHP_EOL;
})();

race

Same effect as promise's race but accepts Promised Generator and Context

usages

yield race((function () {
    echo 'hello ', (yield ctx())->get('hello'), PHP_EOL;
})(), timeout(500), (new Context)->set('hello', 'world'));

all

Same syntax with race

carno-php/coroutine 适用场景与选型建议

carno-php/coroutine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.12k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2018 年 08 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 carno-php/coroutine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 carno-php/coroutine 我们能提供哪些服务?
定制开发 / 二次开发

基于 carno-php/coroutine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 7
  • 依赖项目数: 23
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-16