Swoole Coroutine Channel

Swoole\Coroutine\Channel

Channel for communction between coroutines. Similar to chan in Golang.

Swoole\Coroutine\Channel->__construct(int $capacity = 1)

Construction method.

Swoole\Coroutine\Channel->push(mixed $data) : bool

Write data into channel.

Swoole\Coroutine\Channel->pop(float $timeout = 0) : mixed

Read data from channel.

Swoole\Coroutine\Channel->stats() : array

Get the stats of the channel.

Swoole\Coroutine\Channel->close()

Close channel and resume the other Coroutine.

Swoole\Coroutine\Channel->length() : int

Get the number of items in channel.

Swoole\Coroutine\Channel->isEmpty(): bool

Check if the channel is empty.

Swoole\Coroutine\Channel->isFull() : bool

Check if the channel is full.

Swoole\Coroutine\Channel->$capacity : int

The capacity of the channel.

Swoole\Coroutine\Channel->$errCode : int

Get he error code of the channel.

Example:

<?php
use Swoole\Coroutine as co;
$chan = new co\Channel(1);
co::create(function () use ($chan) {
    for($i = 0; $i < 100000; $i++) {
        co::sleep(1.0);
        $chan->push(['rand' => rand(1000, 9999), 'index' => $i]);
        echo "$i\n";
    }
});
co::create(function () use ($chan) {
    while(1) {
        $data = $chan->pop();
        var_dump($data);
    }
});
swoole_event::wait();