PHP 進(jìn)程鎖定問(wèn)題分析研究
1. 區(qū)分讀鎖定 和 寫 鎖定。
如果每次都使用 寫鎖定,那么連多個(gè)進(jìn)程讀取一個(gè)文件也要排隊(duì),這樣的效率肯定不行。
2. 區(qū)分 阻塞 與 非 阻塞模式。
一般來(lái)說(shuō),如果一個(gè)進(jìn)程在寫一個(gè)文件的時(shí)候,另外一個(gè)進(jìn)程應(yīng)該被阻塞,但是,很多時(shí)候,我們可以先干點(diǎn)別的事情,
然后再判斷一下是否有其他人在寫文件,如果沒(méi)有,再加入數(shù)據(jù),這樣的效率更高。
3. 修復(fù)了 鎖定文件在linux 上的bug,特別是 在 gfs 文件系統(tǒng)上的bug。
代碼如下:
<?php
class File_Lock
{
private $name;
private $handle;
private $mode;
function __construct($filename, $mode = 'a+b')
{
global $php_errormsg;
$this->name = $filename;
$path = dirname($this->name);
if ($path == '.' || !is_dir($path)) {
global $config_file_lock_path;
$this->name = str_replace(array("/", "\\"), array("_", "_"), $this->name);
if ($config_file_lock_path == null) {
$this->name = dirname(__FILE__) . "/lock/" . $this->name;
} else {
$this->name = $config_file_lock_path . "/" . $this->name;
}
}
$this->mode = $mode;
$this->handle = @fopen($this->name, $mode);
if ($this->handle == false) {
throw new Exception($php_errormsg);
}
}
public function close()
{
if ($this->handle !== null ) {
@fclose($this->handle);
$this->handle = null;
}
}
public function __destruct()
{
$this->close();
}
public function lock($lockType, $nonBlockingLock = false)
{
if ($nonBlockingLock) {
return flock($this->handle, $lockType | LOCK_NB);
} else {
return flock($this->handle, $lockType);
}
}
public function readLock()
{
return $this->lock(LOCK_SH);
}
public function writeLock($wait = 0.1)
{
$startTime = microtime(true);
$canWrite = false;
do {
$canWrite = flock($this->handle, LOCK_EX);
if(!$canWrite) {
usleep(rand(10, 1000));
}
} while ((!$canWrite) && ((microtime(true) - $startTime) < $wait));
}
/**
* if you want't to log the number under multi-thread system,
* please open the lock, use a+ mod. then fopen the file will not
* destroy the data.
*
* this function increment a delt value , and save to the file.
*
* @param int $delt
* @return int
*/
public function increment($delt = 1)
{
$n = $this->get();
$n += $delt;
$this->set($n);
return $n;
}
public function get()
{
fseek($this->handle, 0);
return (int)fgets($this->handle);
}
public function set($value)
{
ftruncate($this->handle, 0);
return fwrite($this->handle, (string)$value);
}
public function unlock()
{
if ($this->handle !== null ) {
return flock($this->handle, LOCK_UN);
} else {
return true;
}
}
}
?>
測(cè)試代碼:
<?php
/**
* 進(jìn)行寫鎖定的測(cè)試
* 打開(kāi)線程1
*/
require("file_lock.php");
$lock = new File_Lock(dirname(dirname(__FILE__)) . "/FileLock.lock");
/** 單個(gè)線程鎖定的速度 1s 鐘 3萬(wàn)次。 **/
/** 兩個(gè)線程寫,兩萬(wàn)的數(shù)據(jù) 大概要 7s 鐘*/
/** 一個(gè)線程寫,一萬(wàn)的數(shù)據(jù) 大概要 3.9s 鐘,居然兩個(gè)文件同時(shí)寫,要快一點(diǎn)*/
/** 不進(jìn)行鎖定,一個(gè)進(jìn)程 寫大概要 2.8s 鐘,加鎖是有代價(jià)的。 */
/** 不進(jìn)行鎖定,兩個(gè)進(jìn)程 分布不是很均勻,而且大多數(shù)都沖突 */
$lock->writeLock();
$lock->increment();
$lock->unlock();
while ($lock->get() < 2) {
usleep(1000);
}
sleep(1);
echo "begin to runing \n";
$t1 = microtime(true);
for ($i = 0; $i < 10000; $i++)
{
$lock->writeLock();
$lock->increment(1);
$lock->unlock();
}
$t2 = microtime(true) - $t1;
echo $t2;
?>
我增加了一個(gè) increment 的函數(shù),可以實(shí)現(xiàn)簡(jiǎn)單的線程同步,讓兩個(gè)進(jìn)程同時(shí)執(zhí)行某段代碼,當(dāng)然,這個(gè)有一定的誤差
這里的誤差是 0.001s。
把這個(gè)類簡(jiǎn)單的用到 前面的memcache 消息隊(duì)列中就可以實(shí)現(xiàn) 線程安全的消息隊(duì)列。
相關(guān)文章
PHP計(jì)算2點(diǎn)經(jīng)緯度之間的距離代碼
以下是對(duì)PHP計(jì)算2點(diǎn)經(jīng)緯度之間的距離代碼進(jìn)行了分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08PHP實(shí)現(xiàn)針對(duì)日期,月數(shù),天數(shù),周數(shù),小時(shí),分,秒等的加減運(yùn)算示例【基于strtotime】
這篇文章主要介紹了PHP實(shí)現(xiàn)針對(duì)日期,月數(shù),天數(shù),周數(shù),小時(shí),分,秒等的加減運(yùn)算,結(jié)合實(shí)例形式分析了基于strtotime的簡(jiǎn)單日期時(shí)間運(yùn)算技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04PHP實(shí)現(xiàn)的分頁(yè)類定義與用法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的分頁(yè)類定義與用法,結(jié)合具體實(shí)例形式分析了php封裝的分頁(yè)類常用操作及具體使用技巧,需要的朋友可以參考下2017-07-07PHP常見(jiàn)的幾種攻擊方式實(shí)例小結(jié)
這篇文章主要介紹了PHP常見(jiàn)的幾種攻擊方式,結(jié)合實(shí)例形式總結(jié)分析了php SQL注入、XSS攻擊、文件包含漏洞等php常見(jiàn)攻擊方式,需要的朋友可以參考下2019-04-04