swoole一键协程


swoole4.x后支持一键协程 加上后,开启一键协程化后,MySQLRedisCurl 等操作会变成异步 IO

//此行代码后,文件操作,sleep,Mysqli,PDO,streams等都变成异步IO,见文档"一键协程化"章节
Swoole\Runtime::enableCoroutine();
Co\run(function() {
    // i just want to sleep...
    for ($c = 100; $c--;) {
        go(function () {
            for ($n = 100; $n--;) {
                sleep(1);
                echo $n.PHP_EOL;
            }
        });
    }

    // 10k file read and write
    for ($c = 100; $c--;) {
        go(function () use ($c) {
            $tmp_filename = "/tmp/test-{$c}.php";
            for ($n = 100; $n--;) {
                $self = file_get_contents(__FILE__);
                file_put_contents($tmp_filename, $self);
                echo $tmp_filename.PHP_EOL;
                assert(file_get_contents($tmp_filename) === $self);
            }
            unlink($tmp_filename);
        });
    }
});

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM