swoole4.x后支持一鍵協程 加上后,開啟一鍵協程化后,MySQL
、Redis
、Curl
等操作會變成異步 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); }); } });