swoole4.x后支持一键协程 加上后,开启一键协程化后,MySQL
、Redis
、Curl
等操作会变成异步 IO
//此行代码后,文件操作,sleep,Mysqli,PDO,streams等都变成异步IO,见文档"一键协程化"章节 SwooleRuntime::enableCoroutine(); Co un(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); }); } });