PHP flock() performance, good or bad?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hi all,

I've got a PHP program that I've added flock() to in order to protect
multiple scripts trying to write to the same file. After I added the
flock() to the code the performance of the code went way down. Can
anyone tell me if calling flock() on a file handle is particularly


Re: PHP flock() performance, good or bad? wrote:
Quoted text here. Click to load it

Most likely, the reason is not in the flock() function itself,
but in the fact that multiple scripts (or multiple instances
of the same script) are trying to write to the file.  Since
it is locked, they have to wait until it is unlocked.

You might want to consider changing from storing data in a
file to storing it in a database.  If you are on Unix and
have no plans to deploy on Windows, another alternative,
semaphores, is available to you.


Re: PHP flock() performance, good or bad?


NC wrote:
Quoted text here. Click to load it

Thanks for the response, appreciated. One thing I didn't make clear,
and probably should have, is that writing this file only happens when
this page is called in a 'build' mode. That is the page does two
things, it builds the resulting HTML as a static HTML file and saves
it, and it knows how to display that page. Kind of a home grown caching
system. The end result is that once the static HTML is built, writing
the file shouldn't happen again when a user views the page.


Re: PHP flock() performance, good or bad?

One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable's handwriting:

Quoted text here. Click to load it

Dunno about the performance of the flock() function by itself, but be
sure to check whether there is no other script (or maybe a second
invocation of this script?) that locks the file and causes your script
to wait for an unlock.
What I mean is: you have a script "a.php", which locks the file, writes
to it, and unlocks it. Now imagine a few surfers visit your page one
right after the other. What happens is:
1). Surfer 1 visits your page, a.php locks the file, starts writing;
2). Surfer 2 visits your page, a.php cannot write to the file, waits;
3). Surfer 3 visits your page, same as above.
When  1). finishes to write and unlocks, 2). locks and writes and 3).
still waits. Now, the queue can get really long and the time to wait can
get really huge when you start to get ~20 visits and it will be getting


Re: PHP flock() performance, good or bad?


on 04/21/2005 10:45 AM said the following:
Quoted text here. Click to load it

Flock can be very fast if you are not trying to update the file too
often. You should use shared lock mode for reading and exclusive lock
mode for updating. If you always use exclusive lock for reading it will
be very slow.

You may want to take a look at this class that uses flock to accessing
files to cache content.


Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP /

PHP Reviews - Reviews of PHP books and other products /

Metastorage - Data object relational mapping layer generator

Site Timeline