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 4 7880
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable wr******@charte r.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
write...@charte r.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
Hello,
on 04/21/2005 10:45 AM wr******@charte r.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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Marc |
last post by:
Hello,
when using flock() I get a permission denied error:
Warning: fopen("<filename>", "r+") - Permission denied in <pathtofile>
on line 7
I do this:
$fileToOpen=substr($PHP_SELF, strrpos($PHP_SELF,"/")+1);
$fileHandle=fopen($fileToOpen, 'r+') or die($php_erormsg);
|
by: deko |
last post by:
Do I need to use flock() when reading a file into an array? It's possible that
the file in question could be open at the time the file('filename') request is
made. I realize flock() is required when opening a file with fopen() when there
is contention for the file:
$fp=fopen($ctr, 'w');
//only write if we can get lock on file
if (flock($fp, LOCK_EX))
{
fwrite($fp, "0");
|
by: Rex Karz |
last post by:
Newbie here.
I interpret the fine print at
http://us2.php.net/manual/en/function.flock.php
to mean that flock() does not work where the file being locked is
on an NFS filesystem.
The commentary following the "official" description of flock()
offers numerous circumventions broken flock() where the file
needing atomic access is on an NFS filesystem.
|
by: siliconmike |
last post by:
I've scoured over manual page for flock on http://in2.php.net/flock but
I can't understand a few things:
1. will flock wait until a different process releases the lock ?
2. please clarify this line:
" If you don't want flock() to block while locking, add LOCK_NB "
what does "block" mean above?
|
by: sven.holcombe |
last post by:
Hi there,
I'm trying to just prevent multiple instances of a script running.
I would have thought that the following code would do this for me.
<php
$fp = fopen("foo.txt", "w");
flock($fp, LOCK_EX + LOCK_NB) or die ("Could not get lock!\n");
echo "Got lock!\n";
| |
by: Time Waster |
last post by:
Is this a stupid use of flock:
FILE *fp=fopen(SOME_FILE_CONSTANT,"r+");
flock(fileno(fp),LOCK_EX);
something important here, including reads and a write to fp
flock(fileno(fp),LOCK_UN);
fclose(fp);
Does this accomplish real locking, or just narrow down the
race quite a bit? (Race existing between the unlock and the
|
by: xucs007 |
last post by:
I ran following 2 programs (lock1, lock2) at almost same time,
to write either "123456", or "222" to file "aaa" at the same time.
But I often just got "222456" in "aaa" .
Is this a bug of python fcntl module ? See 2 programs I ran:
#!/usr/bin/env python
import fcntl, time
|
by: Seb |
last post by:
I'm trying to implement a file server using the code below. However
the locking doesn't work. I can delete while put'ing a file.
Anyone got an idea about why?
best regards,
seb
|
by: Bill H |
last post by:
I am wondering if this is possible. I have a file that is accessed by
multiple users and keeps track of activity (the file is polled by
flash every 2 seconds). As users leave, the flash program tells the
php program to remove them from the activity file, and as users access
this activity file, users who have timed out (haven't been heard from
in 60 seconds) are removed. Using a combination of flock, microsecond
sleeps, loops and the flash...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |