Parallel Ajax request and session locking

Yesterday, I was running into a weird issue when I was making parallel ajax requests to my php server running CodeIgniter MVC framework. Let me explain: I had two controller methods A and B. Method A was executing a long action (io and network bound) and was updating database with its progress at regular intervals. Method B was retrieving this progress status from the database and presenting it in html format. I had setup the following jquery ajax calls 

a) A call to method A

b) A call to method B every 2 seconds. 

The idea was to update the UI with the results from method B while A is being performed. This way the user is cognizant of the progress of this long action. The issue I was running into was that method B never returned until method A was complete. When I investigated these requests in chrome I observed that request issued by method B was tagged as “pending” in chrome.

After much research I concluded that the issue was due to php session locks. PHP session doesn’t allow another script to run unless an existing script completes its execution. 


Solution: The solution is to explicitly close the PHP session after you are done with using the PHP session variables. In my case I closed the php session as soon as I entered method  A and re opened it at the end of method B. Note, you won’t be able to update any variables unless you open the session again. 


How to compare mongo ids?

Today, I was trying to compare two mongoids. I thought it would be really straightforward but it took me around 30-40 minutes to figure out how to compare two mongoids. You need to retrieve the “id” param of MongoId objects you wish to compare. Let’s assume user1 and user2 are two instances of MongoID class and you wish to compare the IDs of these two objects. This is how you do it.

if ($user1->{“$id”} == $user2->{“$id”} ) { …

do blah