读取写入上限内容4M,如果不满足需求,需要自行到配置文件修改

异步读取文件:swoole_async_readfile()

/**读取文件**/
//sleep(3);
$res = swoole_async_readfile(__DIR__."/test.txt",function($filename,$fileContent){
	echo "filename:.$filename".PHP_EOL;
	echo "fileContent".PHP_EOL;
	echo $fileContent;
});
sleep(2);
echo "Is me The First?";
var_dump($res);

运行结果:

Is me The First?bool(true)
filename:./swoole/project/swoole1/async/test.txt
fileContent
This a file Test
This is The 2ed line.

异步写入文件:swoole_async_writefile()


$content = date("Y-m-d H:i:s",time()).PHP_EOL;
$res = swoole_async_writefile(__DIR__."/log.txt",$content,function($filename){
	
	echo 'ok'.PHP_EOL;
},FILE_APPEND);


echo 'is my 1st?'.PHP_EOL;
var_dump($res);
echo $content;
echo __DIR__."log.txt";

运行结果:

is my 1st?
bool(true)
2018-10-25 10:14:51
/swoole/project/swoole1/asynclog.txtok

小总结:异步函数部分会在本次运行文件最后执行,但是运行的结果(成功或者失败)可以提前拿到。

异步写入文件,应用到某些关键性的操作,记录log日志,非常高效实用哦!