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";
sleep(10);
flock($fp, LOCK_UN);
?>
The first script to run would obtain the lock on the file, and would
execute.
Any other scripts would fail immediately (because of my non-blocking
LOCK_NB).
But it seems that ALL of my scripts just wait, and ALL of them end up
printing "Got lock!"
Can anyone help me out?
I am on winXP with php5.
Cheers,
Sven. 7 4464
I think you need to open the file with w+ e.g. $fp = fopen("foo.txt",
"w+");
Give it ago see what happends, I might be wrong. sv***********@gmail.com wrote:
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";
sleep(10);
flock($fp, LOCK_UN);
?>
The first script to run would obtain the lock on the file, and would
execute.
Any other scripts would fail immediately (because of my non-blocking
LOCK_NB).
But it seems that ALL of my scripts just wait, and ALL of them end up
printing "Got lock!"
Can anyone help me out?
I am on winXP with php5.
Cheers,
Sven.
sv***********@gmail.com wrote:
>
I'm trying to just prevent multiple instances of a script running.
....
I am on winXP with php5.
Read the documentation:
On some operating systems flock() is implemented at the process
level. When using a multithreaded server API like ISAPI you may
not be able to rely on flock() to protect files against other PHP
scripts running in parallel threads of the same server instance! http://www.php.net/flock
This is the bad news. The good news is that you don't necessarily
need flock() for this. Here's a possible workaround:
$file = 'foo.txt';
if (file_exists($file)) {
$time = date('Y-m-d H:i:s', trim(file_get_contents($file)));
die('Another instance is running since ' . $time);
}
$fp = fopen($file, 'w');
fwrite($fp, time());
fclose($fp);
// the script goes here...
unlink($file);
Cheers,
NC
On 22 Sep 2006 09:30:54 -0700, "NC" <nc@iname.comwrote:
>This is the bad news. The good news is that you don't necessarily need flock() for this. Here's a possible workaround:
$file = 'foo.txt'; if (file_exists($file)) {
$time = date('Y-m-d H:i:s', trim(file_get_contents($file)));
die('Another instance is running since ' . $time); } $fp = fopen($file, 'w');
Although the fun then begins when you hit the (admittedly short-duration) race
condition here...
>fwrite($fp, time()); fclose($fp); // the script goes here... unlink($file);
... or the never-released lock when the script dies before reaching here :-(
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Andy Hassall wrote:
>
$fp = fopen($file, 'w');
Although the fun then begins when you hit the (admittedly
short-duration) race condition here...
You are absolutely right, but how else could you work around the file
locking issue on Windows?
Cheers,
NC
On 23 Sep 2006 09:46:38 -0700, "NC" <nc@iname.comwrote:
>Andy Hassall wrote:
>>
$fp = fopen($file, 'w');
Although the fun then begins when you hit the (admittedly short-duration) race condition here...
You are absolutely right, but how else could you work around the file locking issue on Windows?
Not sure whether there's anything convenient in PHP itself that'll give you a
per-thread reliable and atomic lock. The win32 API has plenty of
synchronisation functions that would work, perhaps one could be called using
the win32api extension?
In some situations where I've needed locks and have had to work across Windows
and NFS filesystems (which have their own file locking quirks) I've used locks
within a database (user-defined locks in Oracle) - but in those cases I was
connecting to a database already.
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Andy Hassall wrote:
>
$fp = fopen($file, 'w');
Although the fun then begins when you hit the (admittedly
short-duration) race condition here...
You are absolutely right, but how else could you work around the file
locking issue on Windows?
Not sure whether there's anything convenient in PHP itself that'll give you a
per-thread reliable and atomic lock. The win32 API has plenty of
synchronisation functions that would work, perhaps one could be called using
the win32api extension?
In some situations where I've needed locks and have had to work across Windows
and NFS filesystems (which have their own file locking quirks) I've used locks
within a database (user-defined locks in Oracle) - but in those cases I was
connecting to a database already.
Thanks for the tips guys, here's a rundown of the end result:
It turns out that whenever I was testing my little example above, I was
opening the script through a browser. For some reason the flock() never
seems to work for me this way, however when I ran it from the
command-line php things sorted themselves out. My application was
going to run in the background anyway, so I've ended up getting it to
work by having my web php call command-line php as a background
process.
The file check/create/unlink option is a good one, but my script is one
that will fire occassionaly, but will sit in a while loop for hours at
a time. So, the dead-script problem is a real one here.
Cheers for the input people,
Sven.
Sven wrote:
>
It turns out that whenever I was testing my little example
above, I was opening the script through a browser. For
some reason the flock() never seems to work for me this
way, however when I ran it from the command-line php
things sorted themselves out.
Well, the reason is that inder Windows, scripts executed by an HTTP
server (such as Apache or IIS) run in threads, so it's possible for
multiple instances of a script to run under the same process ID (even
though the thread ID is different). With command-line execution, no
threading takes place; each instance has its own process ID...
Cheers,
NC 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,...
|
by: Daniel Brunthaler |
last post by:
hi,
i've a problem with file locking.
seems that flock() and gzopen() do not work together. i get the error (or
warning) message ...
Warning: flock(): cannot represent a stream of type ZLIB...
|
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...
|
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...
|
by: writeson |
last post by:
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...
|
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...
|
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);...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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,...
| |