Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Coroutine::isCanceled(int $cid): bool
None
Returns true
if the coroutine was successfully cancelled and false
if there was a problem, use OpenSwoole\Util::getLastErrorCode()
to see what went wrong.
Check if the current coroutine has been cancelled or not.
Since v4.7.0
<?php
use OpenSwoole\Coroutine;
use OpenSwoole\Coroutine\System;
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 === OpenSwoole\Coroutine\Channel::CHANNEL_OK);
assert($chan->push("Hello World [2]", 100) === false);
assert(Coroutine::isCanceled() === true);
assert($chan->errCode === OpenSwoole\Coroutine\Channel::CHANNEL_CANCELED);
echo "Done\n";
});