Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP flock() performance, good or bad?

writeson@charter.net
Guest
 
Posts: n/a
#1: Jul 17 '05
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


Micha³ Wo¼niak
Guest
 
Posts: n/a
#2: Jul 17 '05

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


One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable writeson@charter.net's handwriting:
[color=blue]
> 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?[/color]

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
NC
Guest
 
Posts: n/a
#3: Jul 17 '05

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


write...@charter.net wrote:[color=blue]
>
> 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?[/color]

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

mlemos
Guest
 
Posts: n/a
#4: Jul 17 '05

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


Hello,

on 04/21/2005 10:45 AM writeson@charter.net said the following:[color=blue]
> 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?[/color]

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
writeson@charter.net
Guest
 
Posts: n/a
#5: Jul 17 '05

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


NC,

NC wrote:[color=blue]
>
> 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[/color]

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

Closed Thread


Similar PHP bytes