方案一:
function writeData($filepath, $data){$fp = fopen($filepath,'a');do{usleep(100);}while (!flock($fp, LOCK_EX)); //LOCK_EX 取得独占锁定(写入的程序)进行排它型锁定 获取锁 有锁就写入,没锁就得$res = fwrite($fp, $data."\n");flock($fp, LOCK_UN); //LOCK_UN 释放锁定(无论共享或独占)。fclose($fp);return $res;}
方案二(自己做标记):
function write_file($filename, $content){$lock = $filename . '.lck';$write_length = 0;while(true) {if( file_exists($lock) ) {usleep(100);} else {touch($lock);$write_length = file_put_contents($filename, $content, FILE_APPEND);break;}}if( file_exists($lock) ) {unlink($lock);}return $write_length;}
方案三:
使用队列
