阿贾克斯()调用将无法完成,直到previous阿贾克斯()调用完成?阿贾克斯、previous

2023-09-10 21:17:34 作者:独自快乐

所以,我正在为一个CSV处理脚本进度条。 dnc_scrubber.php 穿过CSV和检查一个电话号码,对数据库,返回匹配和不匹配的数据在不同的文件。 lines.php 返回要处理,而 progress.php 返回多少行是通过工作线的总量。我用这两个数字的创建工作的jQuery的进度函数完成的百分比。

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.

我的问题是,第一个阿贾克斯()调用doProgressBar()没有完成,直到来电 dnc_scrubber.php 结束。为了澄清,当看在Chrome网络监视器,该请求发送到 lines.php 在同一时间请求 dnc_scrubber.php ,但没有收到回应,直到 dnc_scrubber.php 运行完毕。下面是相关code:

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

编辑: 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']));

该CSV处理器递增 $ _ SESSION ['lines_proccessed'] 由一个在检查过程结束时,每行

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

推荐答案

最有可能的,你的服务器每个用户的并发连接数限制为1。或者,你正在使用的会话和第一个脚本有它锁定。第二个脚本将被阻塞,直到第一个释放其会话文件的锁。仅使用在session_start()如果你需要,并尽快你完成了 session_write_close释放锁()吧。

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.

编辑:我不知道这是否会工作,但你可以试试。每次要更新会话,来电在session_start(),更新会话,然后调用 session_write_close()。我不知道,如果你被允许这样做,多次的剧本,但它看起来像它应该工作。

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.