天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP 進程鎖定問題分析研究

1. 區分讀鎖定 和 寫 鎖定。
如果每次都使用 寫鎖定,那么連多個進程讀取一個文件也要排隊,這樣的效率肯定不行。
2. 區分 阻塞 與 非 阻塞模式。
一般來說,如果一個進程在寫一個文件的時候,另外一個進程應該被阻塞,但是,很多時候,我們可以先干點別的事情,
然后再判斷一下是否有其他人在寫文件,如果沒有,再加入數據,這樣的效率更高。
3. 修復了 鎖定文件在linux 上的bug,特別是 在 gfs 文件系統上的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("/", "http://"), 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;
}
}
}
?>

測試代碼:
復制代碼 代碼如下:
<?php
/**
* 進行寫鎖定的測試
* 打開線程1
*/
require("file_lock.php");
$lock = new File_Lock(dirname(dirname(__FILE__)) . "/FileLock.lock");
/** 單個線程鎖定的速度 1s 鐘 3萬次。 **/
/** 兩個線程寫,兩萬的數據 大概要 7s 鐘*/
/** 一個線程寫,一萬的數據 大概要 3.9s 鐘,居然兩個文件同時寫,要快一點*/
/** 不進行鎖定,一個進程 寫大概要 2.8s 鐘,加鎖是有代價的。 */
/** 不進行鎖定,兩個進程 分布不是很均勻,而且大多數都沖突 */
$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;
?>

我增加了一個 increment 的函數,可以實現簡單的線程同步,讓兩個進程同時執行某段代碼,當然,這個有一定的誤差
這里的誤差是 0.001s。
把這個類簡單的用到 前面的memcache 消息隊列中就可以實現 線程安全的消息隊列。

php技術PHP 進程鎖定問題分析研究,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 色多多深夜福利免费观看 | 99视频导航 | 秋霞伦理机在线看片 | BL全肉多攻NP高H | 亚洲一区自拍高清亚洲精品 | 大学生一级毛片免费看 | 日韩欧美视频一区 | 国产麻豆福利AV在线观看 | 国内精品久久久久影院网站 | 免费人成在线观看视频不卡 | 国产亚洲精品久久无亚洲 | 青青草久久伊人 | 黑人干肥婆 | 欧美亚洲曰韩一本道 | 草莓视频在线免费观看 | 校园女教师之禁区 | 国产成人女人视频在线观看 | 人妻中文字幕乱人伦在线 | 久久99国产精品自在自在 | 2022一本久道久久综合狂躁 | 中文字幕欧美日韩VA免费视频 | 娇妻归来在线观看免费完整版电影 | 99热这里只有的精品 | 美女脱光app | free性中国hd护士高清 | 色鲁97精品国产亚洲AV高 | 草莓视频免费在线观看 | 国产主播福利一区二区 | 午夜福利不卡片在线播放免费 | 国产盗摄一区二区三区 | 俄罗斯14一18处交 | 国产黄a三级三级三级 | 色淫阁色九九 | 久久伊人电影 | 7m凹凸国产刺激在线视频 | 这里只有精品在线视频 | 国产又黄又硬又粗 | 四房色播手机版 | 午夜福利试看120秒体验区 | 蜜桃视频无码区在线观看 | 一本道高清到手机在线 |