PHP模拟多进程并发将内容写入文件

function conWrite($filename, $content)
{
	$filename_lock = $filename.'.lock';
	while(1) {
		if(file_exists($filename_lock)) {
			usleep(1000);
		} else {
			touch($filename_lock);//加锁
			$f = fopen($filename, 'w');
			fwrite($f,$content);
			fclose($f);
			unlink($filename_lock);
			break;
		}
	}
	if(file_exists($filename_lock)) {
		unlink($filename_lock);
	}
}

你可能感兴趣的:(PHP)