By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,191 Members | 1,411 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,191 IT Pros & Developers. It's quick & easy.

PHP flock() performance, good or bad?

P: n/a
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
slow?

Thanks,
Doug

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable wr******@charter.net's handwriting:
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
slow?


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
worse.

Cheers
Mike
Jul 17 '05 #2

P: n/a
NC
write...@charter.net wrote:

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 slow?


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.

Cheers,
NC

Jul 17 '05 #3

P: n/a
Hello,

on 04/21/2005 10:45 AM wr******@charter.net said the following:
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
slow?


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.

http://www.phpclasses.org/filecache
--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
Jul 17 '05 #4

P: n/a
NC,

NC wrote:

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.

Cheers,
NC


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.

Doug

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.