Jump to content

Flock - Queing


Lectrician

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.