jebriggsy
jebriggsy

Reputation: 33

.ajax() call won't complete until previous .ajax() call is complete?

So, i'm working on a progress bar for a CSV processing script. dnc_scrubber.php goes through the CSV and checks a phone number against a database, returning matched and unmatched data in separate files. lines.php returns the total amount of lines to be processed while progress.php returns how many lines have been worked through. I use these two numbers to create a percentage of work done for jQuery's progressbar function.

My problem is that the first .ajax() call within doProgressBar() doesn't finish until the call to dnc_scrubber.php is finished. To clarify, when looking at the network monitor in Chrome, the request is made to lines.php at the same time as the request to dnc_scrubber.php, but no response is received until dnc_scrubber.php finished running. Here is the relevant code:

$('#progressbar').progressbar();            


$.ajax({
url: 'dnc_scrubber.php',
type: 'POST',
async: true,
data: querystring,  
success: function(){
    for (i = 0; i < files.length; i++){
        $('#complete').append('<a href="process/MATCHED - ' + files[i] + '">MATCHED - ' + files[i] + '</a><br />');
        $('#complete').append('<a href="process/SCRUBBED - ' + files[i] + '">SCRUBBED - ' + files[i] + '</a><br />');
    }
}                       
});


function doProgressBar(){                   
$.ajax({
    url: 'lines.php',
    async: true,
    dataType: 'json',
    complete: function (rez) {
        lines = JSON.parse(rez.responseText);
        lines = parseInt(lines.lines);

        console.log('dpg1 - lines: ' + lines);

        $.ajax({
            url: 'progress.php',
            async: true,
            dataType: 'json',
            complete: function (rez1) {
                prog = JSON.parse(rez1.responseText);
                prog = parseInt(prog.progress);

                console.log('dpg2 - lines: ' + lines + ' prog: ' + prog);

                if (lines > prog){
                    var bar = (prog / lines) * 100;                                         
                    var bar = Math.round(bar);                                          
                    $('#progressbar').progressbar('option', 'value', bar);
                    setTimeout(doProgressBar(), 1000);
                    console.log('dpg3 - lines: ' + lines + ' prog: ' + prog + ' bar: ' + bar);
                } else if (lines == prog){
                    $('#progressbar').progressbar('option', 'value', 100);
                    console.log('dpg3 - lines == prog');
                }

            }
        });

    }
});
}



setTimeout(doProgressBar(), 100);

Is this normal functionality? Is what I'm trying to do not possible? I'm at a loss... thanks in advance for help

EDIT: lines.php

session_start();
header('Content-type: application/json');
echo json_encode(array('lines' => $_SESSION['lines']));

progress.php

session_start();
header('Content-type: application/json');
echo json_encode(array('progress' => $_SESSION['lines_processed']));

The CSV processor increments the $_SESSION['lines_proccessed'] by one at the end of the checking process for every line

Upvotes: 1

Views: 368

Answers (4)

gilly3
gilly3

Reputation: 91527

Most likely, your server limits the number of concurrent connections per user to 1. Or, you are using sessions and the first script has it locked. The second script will be blocked until the first one releases its lock on the session file. Only use session_start() if you need to, and release the lock with session_write_close() as soon as you are done with it.

Edit: I'm not sure if this will work, but you could try it. Each time you want to update the session, call session_start(), update the session, then call session_write_close(). I'm not sure if you are allowed to do that multiple times in a script, but it seems like it should work.

Upvotes: 2

BNL
BNL

Reputation: 7133

Not sure if this is the exact cause of your problem, but it is something that needs fixed.

Change

setTimeout(doProgressBar(), 100);

To

setTimeout(doProgressBar, 100);

You are calling doProgressBar immediately, not after the timeout is complete.

Upvotes: 2

hanzolo
hanzolo

Reputation: 1150

when reading your .csv file with your PHP code is it locking the file? so if it has to read it twice, it will only read it when the previous code closes the stream?

what happens if you open 2 browsers and run the code at the same time? do both browsers execute at the same time with the same results? or is there a wait on the file?

Upvotes: 0

Frankie
Frankie

Reputation: 25165

Ajax would be assync.

lines.php is probably waiting on dnc_scrubber.php as the first one has locked MySQL.

This is just guessing as we don't have your PHP code but try to run both PHP scripts manually and check if lines.php doesn't wait for MySQL to finish.

Upvotes: 1

Related Questions