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