OpenSwoole\Coroutine\Channel->pop()

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Coroutine\Channel->pop(float $timeout = -1): mixed

Parameters

timeout

The timeout period to wait when the channel is empty, the default will block and wait for data to be available

Return

The return value can be any type of PHP variable, including anonymous functions and resources.

OpenSwoole Coroutine Channel Pop

Description

Reads and removes one item of data from the channel.

Quick Usage Example

Wait and pop up one element from the queue of the channel.

<?php

$chan = new OpenSwoole\Coroutine\Channel(1);

co::run(function () use ($chan) {
    $cid = OpenSwoole\Coroutine::getCid();
    $i = 0;
    while (1) {
        co::sleep(1);
        $chan->push(['rand' => rand(1000, 9999), 'index' => $i]);
        echo "[coroutine $cid] - $i\n";
        $i++;
    }
});

co::run(function () use ($chan) {
    $cid = OpenSwoole\Coroutine::getCid();
    while(1) {
        $data = $chan->pop();
        echo "[coroutine $cid]\n";
        var_dump($data);
    }
});

Notes

When the channel is closed, the execution fails and returns false.

You can use $channel->errCode to access the error code for more detail on what happened.

Last updated on September 4, 2022