Swoole\Coroutine::isCanceled()

Latest version: pecl install openswoole-4.11.1

Declaration

<?php Swoole\Coroutine::isCanceled(int $cid): bool

Parameters

None

Return

Returns true if the coroutine was successfully cancelled and false if there was a problem, use swoole_last_error() to see what went wrong.

Experimental feature, not recommended for production use

Description

Check if the current coroutine has been cancelled or not.

Since v4.7.0


Example

<?php
use Swoole\Coroutine;
use Swoole\Coroutine\System;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

Co\run(function()
{
    $chan = new Coroutine\Channel(1);

    $cid = Coroutine::getCid();
    go(function() use ($cid)
    {
        System::usleep(2000);
        assert(Coroutine::cancel($cid) === true);
    });

    assert($chan->push("Hello World [1]", 100) === true);
    assert(Coroutine::isCanceled() === false);
    assert($chan->errCode === SWOOLE_CHANNEL_OK);

    assert($chan->push("Hello World [2]", 100) === false);
    assert(Coroutine::isCanceled() === true);
    assert($chan->errCode === SWOOLE_CHANNEL_CANCELED);

    echo "Done\n";
});
Last updated on March 31, 2022