note 61921 added to function.flock
| From: | marc dot vanwoerkom at fernuni-hagen dot de | Date: | Wed, 15 Feb 2006 10:00:08 +0000 |
| Subject: | note 61921 added to function.flock | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-103754@lists.php.net to get a copy of this message | ||
I ran into a loop because I just checked for true (= you got the lock) as return value of flock()
and tried again when I got a false.
function naive_wait_for_file($fp) {
while (true) {
if (flock($fp, LOCK_EX)) {
return;
}
$k = rand(0, 20);
usleep(round($k * 10000)); # k * 10ms
}
}
Unfortunately in one case the $fp I put in was invalid, so I always got false and got stuck.
Lesson: check if your $fp is valid before entering the loop, or look closer if you get a false.
function wait_for_file($fp) {
if ($fp === false) {
return;
}
while (true) {
if (flock($fp, LOCK_EX)) {
return;
}
$k = rand(0, 20);
usleep(round($k * 10000)); # k * 10ms
}
}
----
Server IP: 212.124.37.9
Probable Submitter: 217.9.102.2
----
Manual Page -- http://www.php.net/manual/en/function.flock.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61921
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61921&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61921&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61921&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61921&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61921&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61921&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61921&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61921&report=yes
Search -- http://master.php.net/manage/user-notes.php