前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >协程取消 API 的示例代码

协程取消 API 的示例代码

作者头像
沈唁
发布2021-06-25 15:48:54
4310
发布2021-06-25 15:48:54
举报
文章被收录于专栏:沈唁志沈唁志
  1. socket
  2. AsyncIO (fread, gethostbyname ...)
  3. sleep
  4. waitSignal
  5. wait/waitpid
  6. waitEvent
  7. Co::suspend/Co::yield
  8. channel
  9. native curl (SWOOLE_HOOK_NATIVE_CURL)

socket

代码语言:javascript
复制
use Swoole\Coroutine;
use Swoole\Coroutine\Socket;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

run(function() {
    $socket = new Socket(AF_INET, SOCK_DGRAM, 0);
    $socket->bind('127.0.0.1', 9601);

    // server
    $cid = go(function () use ($socket) {
        while (true) {
            $peer = null;
            $data = $socket->recvfrom($peer);
            assert(empty($data) === true);
            assert(($socket->errCode == SOCKET_ECANCELED) === true);
            break;
        }
    });

    // client
    Coroutine::sleep(0.1);
    assert(Coroutine::cancel($cid) === true);
});

AsyncIO

代码语言:javascript
复制
use Swoole\Coroutine;
use Swoole\Event;
use Swoole\Coroutine\System;
use function Swoole\Coroutine\run;

run(function () {
    $cid = Coroutine::getCid();
    Event::defer(function () use ($cid) {
        assert(Coroutine::cancel($cid) === true);
    });
    $retval = System::gethostbyname('www.baidu.com');
    echo "Done\n";
    assert($retval === false);
    assert(swoole_last_error() === SWOOLE_ERROR_AIO_CANCELED);
});

sleep

代码语言:javascript
复制
use Swoole\Coroutine;
use Swoole\Coroutine\System;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

run(function () {
    $cid = Coroutine::getCid();
    go(function () use ($cid) {
        System::sleep(0.002);
        assert(Coroutine::cancel($cid) === true);
        assert(Coroutine::isCanceled() === false);
    });
    $retval = System::sleep(10000);
    echo "Done\n";
    assert($retval === false);
    assert(Coroutine::isCanceled() === true);
    assert(swoole_last_error() === SWOOLE_ERROR_CO_CANCELED);
});

waitSignal

代码语言:javascript
复制
use Swoole\Coroutine;
use Swoole\Coroutine\System;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

run(function () {
    $cid = Coroutine::getCid();
    go(function () use ($cid) {
        System::sleep(0.002);
        assert(Coroutine::cancel($cid) === true);
    });
    $retval = System::waitSignal(SIGTERM);
    echo "Done\n";
    assert($retval === false);
    assert(swoole_last_error() === SWOOLE_ERROR_CO_CANCELED);
});

wait/waitpid

代码语言:javascript
复制
use Swoole\Coroutine;
use Swoole\Coroutine\System;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

run(function () {
    $cid = Coroutine::getCid();
    go(function () use ($cid) {
        System::sleep(0.002);
        assert(Coroutine::cancel($cid) === true);
    });
    $retval = System::wait();
    echo "Done\n";
    assert($retval === false);
    assert(swoole_last_error() === SWOOLE_ERROR_CO_CANCELED);
});

waitEvent

代码语言:javascript
复制
use Swoole\Coroutine;
use Swoole\Coroutine\System;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

run(function () {
    $fp = stream_socket_client('tcp://www.baidu.com:80/', $errno, $errmsg, 1);
    var_dump($fp);

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

    $retval = System::waitEvent($fp);
    echo "Done\n";
    assert($retval === false);
    assert(swoole_last_error() === SWOOLE_ERROR_CO_CANCELED);
});

Co::suspend/Co::yield

Co::yield 用于手动让出当前协程的执行权。此方法拥有另外一个别名:Co::suspend()

代码语言:javascript
复制
use Swoole\Coroutine;
use Swoole\Coroutine\System;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

run(function () {
    $cid = Coroutine::getCid();
    go(function () use ($cid) {
        System::sleep(0.002);
        assert(Coroutine::cancel($cid) === true);
    });
    $retval = Coroutine::suspend();
    echo "Done\n";
    assert($retval === false);
    assert(swoole_last_error() === SWOOLE_ERROR_CO_CANCELED);
});

channel

代码语言:javascript
复制
use Swoole\Coroutine;
use Swoole\Coroutine\System;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

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";
});

native curl(SWOOLE_HOOK_NATIVE_CURL)

代码语言:javascript
复制
use Swoole\Coroutine;
use Swoole\Coroutine\System;
use Swoole\Runtime;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

Runtime::enableCoroutine(SWOOLE_HOOK_NATIVE_CURL);
run(function () {
    $ch = curl_init();
    $code = uniqid('swoole_');
    $url = "http://httpbin.org/get?code=".urlencode($code);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    $cid = Coroutine::getCid();
    go(function () use ($cid) {
        System::sleep(0.01);
        assert(Coroutine::cancel($cid) === true);
        assert(Coroutine::isCanceled() === false);
    });

    $output = curl_exec($ch);
    assert(empty($output) === true);
    assert(swoole_last_error() === SWOOLE_ERROR_CO_CANCELED);
    assert(curl_errno($ch) === CURLE_ABORTED_BY_CALLBACK);
    assert(curl_error($ch) === 'Operation was aborted by an application callback');
    curl_close($ch);
});
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-06-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 沈唁志 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • socket
  • AsyncIO
  • sleep
  • waitSignal
  • wait/waitpid
  • waitEvent
  • Co::suspend/Co::yield
  • channel
  • native curl(SWOOLE_HOOK_NATIVE_CURL)
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档