1,单进程处理队列,一个个处理,需要额外的后台进行处理。
2,文件排它锁,是一个不错的选择。简单的处理并发中奖。
<?php
header("Content-type: text/html; charset=utf-8");
$file = fopen("lock.txt","a+");
$t1 = microtime(TRUE);
if (flock($file,LOCK_EX+LOCK_NB))
{
sleep(1);
fwrite($file,"Write something");
flock($file,LOCK_UN);
echo "Ok locking file!";
}
else
{
echo "Error locking file!";
}
fclose($file);
$t2 = microtime(TRUE);
echo sprintf("%.6f",($t2-$t1));
?>