/** * 多进程确保写入一个文件成功,加锁进行 * @param $errorInfo * * @return bool */ function errorLog($errorInfo){ $file_prex = date('Y-m-d',time()); $file = $file_prex.'.log'; $fp = fopen("./log/".$file,'a+'); //最大重试次数 $maxTry = 10; $numTry = 0; do{ $flock = flock($fp,LOCK_EX); if ($numTry > 0) { usleep(100); } $numTry++; }while(!$flock && $numTry<=$maxTry); if ($flock) { fwrite($fp, date('Y-m-d H:i:s').": ".$errorInfo." "); flock($fp,LOCK_UN); } fclose($fp); }