note 87198 added to function.flock
| From: | Francesco at osu1 dot php dot net | Date: | Mon, 24 Nov 2008 10:45:33 +0000 |
| Subject: | note 87198 added to function.flock | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146855@lists.php.net to get a copy of this message | ||
Last example is wrong. Regarding to * in code :
if flock is not obtained but was acquired in exclusive mode, this flock will hang waiting for the
lock to be released. Using LOCK_NB would enter that if block.
Correct me if i'm wrong.
<?php// waiting until file will be locked for writing (1000 milliseconds as timeout)
if ($fp = fopen($fileName, 'a')) {
$startTime = microtime();
do {
$canWrite = flock($fp, LOCK_EX);
// If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load *
if(!$canWrite) usleep(round(rand(0, 100)*1000));
} while ((!$canWrite)and((microtime()-$startTime) < 1000));
//file was locked so now we can store information
if ($canWrite) {
fwrite($fp, $dataToSave);
}
fclose($fp);
}?>
----
Server IP: 213.92.11.19
Probable Submitter: 194.184.159.6
----
Manual Page -- http://www.php.net/manual/en/function.flock.php
Edit -- https://master.php.net/note/edit/87198
Del: integrated -- https://master.php.net/note/delete/87198/integrated
Del: useless -- https://master.php.net/note/delete/87198/useless
Del: bad code -- https://master.php.net/note/delete/87198/bad+code
Del: spam -- https://master.php.net/note/delete/87198/spam
Del: non-english -- https://master.php.net/note/delete/87198/non-english
Del: in docs -- https://master.php.net/note/delete/87198/in+docs
Del: other reasons-- https://master.php.net/note/delete/87198
Reject -- https://master.php.net/note/reject/87198
Search -- https://master.php.net/manage/user-notes.php