Lectrician Posted August 25, 2014 Share Posted August 25, 2014 Hi. If I use Flock to lock a file once open, when other people run the script (visit webpage), will they queue in turn to access the file, eventually being able to access it? Is there a time out? I have been using an IF argument, so if no lock is made, the script continues. Looking at my code, this is undesirable. I would prefer the script to wait until the script can use the file. There are times when several people may be trying to access the same file - The script should run and complete in seconds. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/290637-flock-queing/ Share on other sites More sharing options...
ginerjm Posted August 25, 2014 Share Posted August 25, 2014 Unless your script intentionally 'completes in seconds" you should not have a problem except in very extreme circumstances. Scripts that do not involve tedious processes involving lots of data accesses or queries (not wrapped around a 'flock') will usually complete in much less than a second. That means you would have to have many users hitting your app regularly to reach a true impass. As long as your script is well writeen and doesn't put an unduly long lock on a resource, I would not expect any collisions. The lock is merely a tool to ensure that your script succeeds in posting its results. Quote Link to comment https://forums.phpfreaks.com/topic/290637-flock-queing/#findComment-1488857 Share on other sites More sharing options...
mogosselin Posted August 25, 2014 Share Posted August 25, 2014 Hi Lectrician! In the official PHP guide about the flock function, it says: By default, this function will block until the requested lock is acquired; this may be controlled (on non-Windows platforms) with the LOCK_NB option documented below. To test it, simply add a sleep(30) after acquiring the lock and before releasing it. While it sleeps, open another tab and ask for the same page. The second time you request the page while the first one is sleeping, it should wait until the first page release the lock and continue. Quote Link to comment https://forums.phpfreaks.com/topic/290637-flock-queing/#findComment-1488859 Share on other sites More sharing options...
Lectrician Posted August 25, 2014 Author Share Posted August 25, 2014 Thanks. I am going to remove the IF statement to force all runs of the script to take action on the file. As it stands at the moment, if a lock is not obtained, the script does not take any action on the file. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/290637-flock-queing/#findComment-1488871 Share on other sites More sharing options...
kicken Posted August 25, 2014 Share Posted August 25, 2014 An flock call may fail due to various reasons. You should always check for success before attempting to use the file. Under normal operation the call will eventually succeed and your script can continue. In abnormal conditions something may cause it to fail in which case your script should either re-try or just close the file an abort. There's not much point in calling flock at all if you're just going to ignore it's result and do whatever you want with the file anyway. Quote Link to comment https://forums.phpfreaks.com/topic/290637-flock-queing/#findComment-1488880 Share on other sites More sharing options...
Lectrician Posted August 25, 2014 Author Share Posted August 25, 2014 So if I do: If(flock($file, LOCK_EX)){ Play with file } What happens if a file is already locked. Will it wait, or just pass this code completely? Quote Link to comment https://forums.phpfreaks.com/topic/290637-flock-queing/#findComment-1488892 Share on other sites More sharing options...
Jacques1 Posted August 25, 2014 Share Posted August 25, 2014 The script waits until it's allowed to lock the file. Quote Link to comment https://forums.phpfreaks.com/topic/290637-flock-queing/#findComment-1488895 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.