PHPackages                             carno-php/coroutine - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. carno-php/coroutine

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

carno-php/coroutine
===================

1.0.1(6y ago)81.1k520MITPHPPHP ^7.1

Since Aug 28Pushed 6y ago2 watchersCompare

[ Source](https://github.com/carno-php/coroutine)[ Packagist](https://packagist.org/packages/carno-php/coroutine)[ RSS](/packages/carno-php-coroutine/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (2)Versions (3)Used By (20)

Coroutine - component of carno-php
==================================

[](#coroutine---component-of-carno-php)

Installation
============

[](#installation)

```
composer require carno-php/coroutine
```

Concepts
========

[](#concepts)

> [Cooperative multitasking using coroutines (in PHP!)](https://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutines-in-PHP.html)

### yield values

[](#yield-values)

#### Generator

[](#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

[](#promised)

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

#### Syscall

[](#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
=========

[](#functions)

> namespace in `Carno\Coroutine`

#### go

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#ctx)

Get job's ctx in coroutine

**usages**

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

#### defer

[](#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

[](#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

[](#all)

Same syntax with `race`

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~331 days

Total

2

Last Release

2486d ago

PHP version history (2 changes)1.0.0PHP ^7.2

1.0.1PHP ^7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/ee5620a146ee71289cb4ad63d06cdfcbfd679eb02b446f78bd772df8342b43a0?d=identicon)[moyo](/maintainers/moyo)

---

Top Contributors

[![moolex](https://avatars.githubusercontent.com/u/1132321?v=4)](https://github.com/moolex "moolex (7 commits)")

---

Tags

asynccoroutinenativephp7yield

### Embed Badge

![Health badge](/badges/carno-php-coroutine/health.svg)

```
[![Health](https://phpackages.com/badges/carno-php-coroutine/health.svg)](https://phpackages.com/packages/carno-php-coroutine)
```

###  Alternatives

[2tvenom/cborencode

CBOR encoder for PHP

6213.1M17](/packages/2tvenom-cborencode)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
