Forum Moderators: phranque
when I run file.php?x=a in one window, and then in another I load file.php?x=b in another, the second waits (five minutes) until the first is finished before executing.
What controls this behavior (server or browser) and how can I get them both to load for the same user at the same time?
Thanks for any help. This is holding up a site launch :(
You should test from two different machines, just to eliminate any possible client-side blocking, but I strongly suspect the blocking is taking place on the server side. An in-depth review of the file-locking mechanism used by your scripts will likely be needed, and more to the point, a review of when and why the database must be locked will be needed.
A good approach is to put off opening the database until absolutely all preliminary work has been done: Get all of the database keys pre-calculated, and pre-calculate all read pointers and as many write pointers and values as possible. Then open and lock the database, read what you need, calculate the write values, write them and immediately close the database... Basically, "get in and out as fast as possible."
Beware of changing the file-locking mechanism(s) without fully understanding why locking is needed; If you change the locking, you can seriously mess up your database, because one user's thread can read the database, and (for example) find that an item is in stock. Then another user may come in and do the same, but this second user proceeds directly to checkout and orders the item, reducing your stock to zero (out of stock). The first user, after further shopping, also proceeds to checkout, and orders that same item, which his "copy" of the database said was in stock. But the second user has already ordered the last one... So, you now have a user who is going to be disappointed, and possibly, an in-stock count of -1 on that item!
I don't know anything about your site or your scripts, so the above are all just generalizations and examples. Please don't mistake me for an expert on your detailed site-specific problem... :)
Jim
Thanks as always for your advice, but I've solved this issue (only to raise others I'm afraid).
After much browbeating, I have some answers for anyone who stumbles upon this thread in the future:
My tests have shown that apache will not load the same file at the same time if they have the same name unless they are followed by different query strings.
Even more importantly (and this is probably your case) if you are using
file.php?id=1 and
file.php?id=2
These will not load simultaneously if you are using sessions. You must close the session for writing (session_write_close) before the other file will load.
Took me a week to work this out. I hope this helps someone.
PHP is obviously using file write-locking. This is true on any OS, since two processes cannot write the same data area at the same time, even if they can read the same data area at the same time. Read locking is required if two users can read a file, make changes to the same part(s) of that file, and then write the results. The reason is that without full read/write locking, these two time-sequence scenarios have different outcomes:
User A reads data
User A locally edits data
User A writes edited data
User B reads data
User B locally edits data
User B writes edited data
(This always works)
User A reads data
User A locally edits data
User B reads data
User A writes edited data
User B locally edits data
User B writes edited data
Here, User B does not "see" the changes made by User A, and User B's changes will overwrite or discard User A's changes. This is why full read/write locking is required if any user can modify any arbitrary part of a file or database. Sophisticated DBMS packages can lock individual records to reduce collisions and the resulting delays, but if you have a simple application, you will need to rely on locking the whole database or data file.
Jim