Simultaneous AJAX Requests and PHP Sessions
I'm having a peculiar problem with making simultaneous开发者_如何学编程 AJAX requests that modify a PHP object stored in the session. I'm having problems with race conditions but I don't understand why they're happening... only one AJAX call can have the session open at a time, so if they keep the session open while making modifications, shouldn't each call be able to see the changes made by the others? Here's a simplified version of the flow (A and B are two sets of parameters):
1) An AJAX call is made which runs Initiate(A). This call opens the session. It updates the PHP object by adding "A - Running" to an array.
2) An AJAX call to Run(A) is made. It waits until Initiate(A) closes the session and then runs. It briefly opens the session but explicitly closes it and does not modify the object. Run(A) will take up to 60 seconds to run. 3) Another AJAX call is made which runs Initiate(B). This opens the session and modifies the object by adding "B - Running" to the array. 4) Another AJAX call is made to Run(B). It opens the session briefly but explicitly closes it.So far, everything is great. Run(A) and Run(B) are both executing. Both "A - Running" and "B - Running" are in the object's array, stored in the session which is not open in either request. Here's where things get funny:
5) Run(A) finishes executing first and an AJAX call is made which runs Show(A). This opens the session, retrieves the object, and changes "A - Running" to "A - Complete". Funny thing is, "Running - B" is not there at all.
6) When Run(B) finishes, an AJAX call is made that executes Show(B). This opens the session and retrieves the object. It changes "B - Running" to "B - Complete". The first element in the array, however, is "A - Running".If the session is closed and saved, when Show(A) opens it, why can't it see the changes made by Initiate(B)? And when Initiate(B) can clearly see the changes made by Initiate(A), but Show(B) can't see what Show(A) did...
Similar problems happen if Run(B) finished before Run(A).
It appears to be related to the eyeOS framework. Looks like since I do a session_write_close() at the beginning of my Run function, I have to session_start() at the end of my Run function... otherwise eyeOS isn't able to clean up properly or something.
I've been working on this on and off for 3 days. Moral of the story: don't use eyeOS for developing!
精彩评论