-1

In a web application that interacts with one server, there is a jQuery Ajax call with a PHP script that takes about 20 seconds to execute.

$(document).ready(function() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: 'createPDF.php',
        data: str,
        success: function(response) {
            showPDF(response);
        }
        error: function() {
            console.log('ERROR WHEN CREATING PDF');
        }
    });
});

In the meantime a user ask to the application to get a list (getList.php) through another jQuery Ajax independant from createPdf.

$(document).ready(function() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: 'getList.php',
        data: str,
        success: function(response) {
            showList(response);
        }
        error: function() {
            console.log('ERROR WHEN GETTING LIST');
        }
    });
});

The problem is that getList.php only starts executing once createPDF.php finishes.

The application works with sessions, and tests have been done in separate clients, in which case both requests run parallel, but in the same computer they don't.

In this scenario how to run both requests parallel?

Thank you

mikl
  • 1,067
  • 1
  • 20
  • 34
  • did you try using `async :` option as true? – Imesh Chandrasiri Mar 06 '15 at 03:50
  • I tryed, but it is set by default anyways, so it made no difference – mikl Mar 06 '15 at 03:52
  • Are you SURE it's executing only after the subsequent call? By any chance, is `viewPdf()` what makes the call to getList.php? If so, that's your problem. – kylehyde215 Mar 06 '15 at 04:32
  • They are totally indepoendant processes, they only share the server, and it is happening when different clients work at the same time too, they interfer each other the same way – mikl Mar 06 '15 at 04:49
  • By any chance is it a problem caused by the VPS server stacking executions? – mikl Mar 06 '15 at 04:52
  • Can post `js` that requests `getList.php` ? – guest271314 Mar 06 '15 at 04:59
  • For sure, added it, as mentioned it is pretty much similar to the one of createPdf.php – mikl Mar 06 '15 at 05:04
  • How is `$.ajax()` called ? – guest271314 Mar 06 '15 at 05:08
  • A general outline would be (documentready > ajax(saveFile) > ajax(createPdf)) && (documentready > ajax(getList)). Only createPdf is inside another ajax, getList is independant (need getList to execute whilst createPdf does and not only after it finished) – mikl Mar 06 '15 at 05:16
  • 1
    have you tried removing all the logic from the php-script and temporarily just let the server wait for x seconds to simulate the current situation, just interested if that problem remains or if something else is wrong !? – luk2302 Mar 07 '15 at 23:30
  • 1
    And have you seen [this question/answer](http://stackoverflow.com/questions/6903318/multiple-ajax-requests-delay-each-other?rq=1)? – luk2302 Mar 07 '15 at 23:33

1 Answers1

1

Basically what is happening is that on your server the session file is locked by the first script, e.g. saveFiles. After that script has completed its task, the session is unlocked again. Unfortunately your script getList wants to access the same session, since it is locked, getList will have to wait until it is unlocked.

One solution for this is to call session_write_close() after all writing to session data has been done, that will cause the session file to be unlocked and be used by the next script. That call should obviously happen as soon as possible after session_start() to minimize the waiting time for other requests.

For a more detailed explanation have a look at https://stackoverflow.com/a/6933294/2442804

Community
  • 1
  • 1
luk2302
  • 55,258
  • 23
  • 97
  • 137