Multiple - Simultaneous Http Requests[Solved]

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.

In index.php, surely you have those ajax requests the wrong way around? I thought you were calling the long one first and then the short?

It’s late so not testing right now, but I wonder if somehow the async:true flag isn’t working (even though default is true)?

Interesting… what version of Yii and JQuery are you using?

You’re right, I updated my post to reflect the error. I am currently running Yii verison 1.1.8, jQuery version 1.6.1.

It is possible that the async is not working, but if I run the request manually (just pull them in a browser) they still run in whatever sequence I run them, which I thought was odd. I know that if you request the same page this happens, but usually when requesting two different pages I’ve been able to get async responses (never in yii though). This is why I tried placed the different request in different controllers. Tomorrow I’ll try sending the 2nd request to a seperate Yii app and see if that bears better fruit.

Dear Friend

These are all the versions I am using in my localhost.

Yii:1.1.13.

Jquery:1.8.3.

This is the view.

aync.php




<?php

echo CHtml::ajaxButton("Test One",'',array(

    "update"=>"#test1",

    'data'=>array("test"=>"one"),

	),array("id"=>"async1"));


	echo "</br>";

	echo "</br>";


echo CHtml::ajaxButton("Test Two",'',array(

    "update"=>"#test2",

    'data'=>array("test"=>"two"),

	),array("id"=>"async2"));


?>




<div id="test1"></div>

<div id="test2"></div>




This is the controller




public function actionAsync()

{   

	if(isset($_GET['test']))

	{

		if($_GET['test']=='one')

			echo "test1";


		if($_GET['test']=='two')

		{   

			sleep(10);

			echo "test2";

		}


		Yii::app()->end();

	}

	

        $this->render("async");

}



I found no problems in aynchronous response.

If I click first button immediately clicking second button, test1 is displayed immediately.

test2 is displayed after 10 secs.

I also placed request in separate actions, still it works.

I am still not able to comprehend your problem.

Regards.

Well, just to be certain I updated to the latest version of Yii, so I am not running 1.1.13 and jQuery verion 1.8.3. I tried using your example exactly, and I still have the same issue. If I hit the "Test Two" button and then "Test One" they both return at the same time.

It could be a server issue, or maybe another script that I have running on the machine. I"ll keep investigating. Thanks for the help so far.

Update:

If, on the long script I add session_write_close() before doing work on the long running function it behaves correctly. So it appears to be a session issue (which explains why making a request for a non-yii script, which wasn’t using any session, worked). I’m not sure how secure this method is, but at least it solves the immediate problem.

Is the answer on this page the same problem as yours? It might be related to file based sessions.

EDIT: Ninja’d

Yes, thank you.