I am working on a project that requires the uploading of an excel file,the subsequent parsing of that file and finally record creation / updating based on the contents of that file. This ends up being a fairly time consuming process and I want to be able to allow user to view the current progress. I was planing to use ajax for this, but I’ve run into a problem. Apparently, under my current settings, Yii only allows for a single http request to process at a time. If I send multiple ajax requests then the first one must complete before the second request is processed. I did a little experimentation and it seems to be the case that this is not an ajax issue at all, but deals with the number of requests that Yii will let me do at a time. For example, if I open a long-loading page in one tab, then another page in a separate tab, then the first page must first load before the second one will even start. At first I thought that this was an issue with my web-server settings but when I try and open a web page that is not in yii (ie, a random script just sitting in the web path) then I am able to make two request that operate concurrently instead of in sequence.
Example:
if i have a the following files:
testControllerA:
class testControllerA extends Controller
{
actionAjaxTestShort()
{
echo CJSON::encode(array('status'=>'success', 'message'=>' short time = done : ' .
date('G:i:s')));
}
}
testControllerB:
class testControllerB extends Controller
{
actionAjaxTestLong()
{
//simulate long-running process
for($i=0;$i<30;$i++)
sleep(1);
echo CJSON::encode(array('status'=>'success', 'message'=>' long time = done : ' .
date('G:i:s')));
}
}
mainController.php:
class mainController extends Controller
{
actionIndex()
{$
this->render('index');
}
}
index.php:
<script>
function testAjax()
{
$.ajax({
url: '<?php echo $this->createUrl('testControllerB /testLong'); ?>',
data: {},
dataType: "json",
type: 'POST',
async: true,
success:
function(data){
if(data.status == "success")
{
alert(data.message);
}
}
});
$.ajax({
url: '<?php echo $this->createUrl('testControllerA /testShort'); ?>',
data: {},
dataType: "json",
type: 'POST',
async: true,
success:
function(data){
if(data.status == "success")
{
alert(data.message);
}
}
});
}
</script>
<?php
echo CHtml::button('Run', array('onclick'=>'testAjax();'));
?>
With this setup, if I hit the run button, first the long-request fires and only after it has finished completely (30 seconds later) does the next request get a response. Looking in firebug the both ‘fire’ at the same time, but the responses come back in squence. I am trying to allow for them to run concurrently. If I change the address of the 2nd ajax call’s target to a non-yii script, for example http://localhost/quickresponse.php, it return instantly so I think this has to do with how Yii is handling the request.
Obviously this is just example code to demonstrate the issue I’m having. This is about as simple as I could make it. Any help would be greatly appreciated. I apologize if this has already been answered ( I wasn’t able to find anything like this on the forums). If it has, I’d appreciate a point in the right direction. Thanks in advance for your time.