Re: usage of flock
| From: | Myron Turner | Date: | Sat, 24 Mar 2007 03:34:25 +0000 |
| Subject: | Re: usage of flock | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-251268@lists.php.net to get a copy of this message | ||
Richard Lynch wrote:
On Fri, March 23, 2007 7:52 pm, Yvan Strahm wrote:It's not clear to me from the original question, Yvan, whether you are able to get any lock at all. That is, if you are using LOCK_NB and a lock is already on the locked file, then the lock will be refused and the call will return immediately without a lock. So, the only way to use LOCK_NB is in a loop. Generally, this is not recommended, because in the time the loop gets back to making a second call, another file might have gotten the lock. However, in your case this might not matter, since you are the only one sending out these job controllers, and eventually all your processes will have had a chance at the lock file. On the other hand, if you use LOCK_EX, the call will block until a lock is available. Again, in your case this might not matter either. The danger here is that you will get a process that doesn't shut down. You can deal with this by setting an alarm which is caught using a signal handler that can exit the process. If you use LOCK_NB with a loop, you can also break out of the loop and exit after a time as well. You'd probably want to use sleep to time your loop, not a counter, since the counter might swallow up cpu. -- _____________________ Myron Turner http://www.room535.org http://www.bstatzero.org http://www.mturner.org/XML_PullParser/I am confused with the flock function and its usage. I have jobs which are stored in a database, these jobs are run by a series of job_runners scripts but sometimes the job_runners stop ( server or php crash-down). So i put a job_controller in crontab to check regularly if the runners run. But after a while I have a bunch of job_controller running, so to avoid that I tried to use flock. I try to put this in the job_controller: $wouldblock=1; $f=fopen("controller.lock", "r"); flock($f, LOCK_EX+LOCK_NB, $wouldblock) or die("Error! cant lock!"); hoping that as long as the first job_controller run or don't close the file handle, a second job_controller won't be able to lock the controller.lockfile and die, but it didn't work. I also try this: $wouldblock=1; $f=fopen("controller.php", "r"); flock($f, LOCK_EX+LOCK_NB, $wouldblock) or die("Error! cant lock!"); hoping the first job_controller will lock it-self, but it didn't work. I also thought of writing in the lock file the PID of the first job_controller and then compare it and if it doesn't match then die, but my main concern is , if the server crash down the "surviving" lock file will prevent any job_controller to start. So how could prevent multiple instance of the same script? Is flock the best way?You can do it with flock, but then you end up sooner or later with a locked file from an "exit" or killed script, and then you have to know to remove locks older than X minutes. You could also just do a "mkdir" for your lock, and check its filemtime. You could even use "touch" within loop of the script to make sure the script is still going, and safely assume that any lock older than X seconds is stale and can be ignored/removed. I've never used locks in PHP, but have used them in Perl. In Perl a lock is automatically released on exit or when the locked file is closed. Is that not the same in PHP? According the the man page for the C version of flock, it too releases the lock on close and C's exit closes all streams. So, Perl is consistent with that. Just wondering for myself it this isn't the case with PHP, in case I ever want to use a lock.