Swoole\Coroutine::isCanceled()

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

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 August 31, 2022