2

I'm using codeigniter. Following is my AJAX code in view -

jQuery(document).ready(function () {
    $.ajax({
         type:'post',
         data:'',
         url:HOST+'/exp1',
         success:function(data)
         {
             alert('here1')
         }
    });

    setInterval(function(){
         $.ajax({
              type:'post',
              data:'',
              url:HOST+'/exp2',
              success:function(data)
              {
                   alert(data)
              }
         });
    }, 1000);
});

And the controller code is -

function exp1()
{
    sleep(10);
}

function exp2()
{
    echo 'test';
}

Above both ajax calls are independent but on same controller. First ajax takes 10 seconds to get response, while second ajax will take 1 second to get response.

Second ajax request is fired after every 1 second.

But when i execute the code, i did not get the response from the second ajax until to be get first ajax response.

How can i drive both ajax simultaneously so that i'll get response from second ajax while first ajax call will be in process ?

Ganesh Gadge
  • 366
  • 2
  • 17
  • Is your server configured to only accept a single request at a time? – freedomn-m May 08 '18 at 11:01
  • No, and i'm running this code on wamp. – Ganesh Gadge May 08 '18 at 11:12
  • 1
    unlikely to be the issue, but change your `alert` to `console.log` and look in the browser console as `alert` locks all the browser's threads. – freedomn-m May 08 '18 at 11:18
  • Tried same, but doesn't make any difference. – Ganesh Gadge May 08 '18 at 12:03
  • This is exactly the code which are you testing? In this scenario, the second ajax will not wait for the first. If you have sql queries on both controller on the same table, and in the exp1 function the table used is locked for 10 seconds, then the quuery from exp2 will wait for locking table – vasilenicusor May 08 '18 at 12:13

1 Answers1

2

Session blocking problem.

In exp1() - calling session_write_close() before sleep(10) should prevent delay on other concurrent page requests.

"By default PHP writes its session data to a file. When you initiate a session with session_start() it opens the file for writing and locks it to prevent concurrent edits."

Two simultaneous AJAX requests won't run in parallel

"Long story short - call session_write_close() once you no longer need anything to do with session variables."

https://www.codeigniter.com/user_guide/libraries/sessions.html#a-note-about-concurrency

Karlo Kokkak
  • 3,674
  • 4
  • 18
  • 33