Swoole\Coroutine::isCanceled()

Experimental feature, not recommended for production use

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.


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::sleep(0.002);
        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";
});