I have an PHP application that receives a lot of ajax calls.
I've noticed that, when two or more calls happen at the same time they are not executed concurrently, the first one has to stop so the second one executes.
I've built a small test to try fixing this issue. I have an html like this:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<a href="test.php">Test</a>
<button>Click</button>
<button>Click</button>
<button>Click</button>
<button>Click</button>
<script type="text/javascript">
$('button').click(function() {
$.get("test.php");
});
</script>
</body>
</html>
The test.php contains:
<?php
sleep(2);
echo 'Test';
?>
So, after executing $('button').click() on developer tools (or firebug) console the requests happen at the same time but return the answers 2 seconds after the last one, instead of all four returning at the same time (2 seconds later).
Well, I tried opening in other browsers, at the same time, this file and it worked, it got executed concurrently. I mean, running this on chrome and in firefox opening the page in a tab show the 'Test' in 2 seconds while the responses from chrome ajax requests come 2 seconds after the last request finished, so other browser request has no effect on the first one call (that would prove this to be a session problem, but see below).
The same happens if I open multiple tabs for test.php.
I've tried changing session to memcache - People said, in other questions, that the session could be locking. It makes a lot of sense but even after changing from files to memcache the problem persists (and the script has no session, and session_autostart is off)
I'm really thing about a configuration problem with apache, since in the first time after a while, if I execute the script it will execute all 4 ajax calls concurrently! I'm thinking about some DoS protection or something like this.
Anyway, any help would be much appreciated!
Ok, solved the problem.
It was a session locking issue after all, problem is that memcached locks the session too!
Had to install php_memcached 2.0.1 that has an lock flag in memcached.ini and use that version. Solved the problem.
Although when a request is for the same url (in the example above I used test.php 4 times) it still executes one at a time. I'm pretty sure that's the right behavior since it doesn't make much sense to ask for a resource many times at the same time. Changed my example to request for test1.php, test2.php and so on and it worked just fine. Also works if the querystring is changed.
Thanks for your help!
PS: Don't have reputation to answer my own question so early so here is the answer
in my case the function:
session_write_close();
solved my problem
I have the next program, a main page that call recurrently one ajax file called ajax.php. It is called every "n" seconds.
And, my form have a button that return a report that it's called via ajax (** reporte.php**)It is called only by request of the user (click) and it could take a minute to generate the result.
Most of the time, ajax.php takes 20ms to run but, if report.php is running the ajax.php is freeze until reporte.php finish the task. And worst, ajax.php could be stacked.
reporte.php (before)
<?php
// Some PHP Code goes here.
// The rest of the PHP code (mostly, the slow part of the process).
?>
reporte.php (after the fix)
<?php
// Some PHP Code goes here. (ideal if it is the fast part).
session_write_close(); // fix for concurrent ajax
// The rest of the PHP code (mostly, the slow part of the process).
?>
Before the solution (a screenshot of Firefox's firebug plugins):
After the solution
Check how before the solution, reporte.php and ajax.php both are still loading (and ajax has been stacked in several calls). And how after the solution, reporte.php is loading while every consecutive ajax calls is already loaded.
Take note that after session_write_close() function, it is not possible (or recommended) to use any session function and value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With