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
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 withsession_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 callsession_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.