Javascript 如何在PHP中对请求进行排队?

Javascript 如何在PHP中对请求进行排队?,javascript,php,ajax,http,xmlhttprequest,Javascript,Php,Ajax,Http,Xmlhttprequest,我尝试了下面的代码来排队请求,但它没有按预期工作 <?php $Sleep_Time = "10"; if (isset($_POST["String"])){ $File = "Edit_File_Content.txt"; while(file_exists($File . "_Locked")) { //wait, do nothing until "Edit_F

我尝试了下面的代码来排队请求,但它没有按预期工作

<?php

$Sleep_Time = "10";

if (isset($_POST["String"])){

$File = "Edit_File_Content.txt";

    while(file_exists($File . "_Locked")) {
        //wait, do nothing until "Edit_File_Content.txt_Locked" file is deleted
    }

file_put_contents($File . "_Locked", "");       //create new file with same name with "_Locked" in the end (second parameter must be specified)

$File_Content = file_get_contents($File);

$File_Content .= $_POST["String"];

sleep($Sleep_Time);       //sleep for x seconds

file_put_contents($File, $File_Content);

unlink($File . "_Locked");                  //delete the above "_Locked" file

echo "String Added";
return;
}
?>

Each request takes <?php echo $Sleep_Time ?> seconds to finish!
<br><br>
<input type="button"  value="Add A" onclick='Add_String("A")'>
<input type="button"  value="Add B" onclick='Add_String("B")'>
<br><br>
<div id="Ajax_Response">Ajax Response:<br><br></div>


<script>

function Add_String(Option){      //____________________________

var http = new XMLHttpRequest();
http.open('POST', "");      //blank url (send to same page)

http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');     //necessary to send "String" POST key below to php

    http.onreadystatechange = function(){
        if (this.readyState === 4) {     //"4", request finished and response is ready!
        document.getElementById("Ajax_Response").innerHTML += this.responseText + "<br>";
        }
    };

http.send('String=' + Option);
}

</script>

每个请求需要几秒钟才能完成!




Ajax响应:

函数Add_字符串(选项){//____________________________ var http=new XMLHttpRequest(); http.open('POST',“”);//空白url(发送到同一页面) http.setRequestHeader('Content-type','application/x-www-form-urlencoded');//将下面的“String”POST键发送到php所必需的 http.onreadystatechange=函数(){ 如果(this.readyState==4){/“4”,请求完成,响应准备就绪! document.getElementById(“Ajax_响应”).innerHTML+=this.responseText+“
”; } }; send('String='+选项); }
下图显示了“添加A”和“添加B”同时单击5次时的“Ajax响应”:

所有请求完成后,“Edit_File_Content.txt”文件将只包含“AA”而不是“aaaabbbbb”字符串

关于如何在php中有效地对请求进行排队有什么建议吗?

我编写了下面的“Check\u File\u Write\u queue()”函数,到目前为止,它似乎工作正常

当“添加A”和“添加B”同时单击5次时,当所有请求完成后,“编辑文件内容.txt”文件将按预期包含“AAAAA BBBBB”字符串

<?php

$Sleep_Time = "10";

if (isset($_POST["String"])){

$File = "Edit_File_Content.txt";

    while(file_exists($File . "_Locked")) {
        //wait, do nothing until "Edit_File_Content.txt_Locked" file is deleted
    }

file_put_contents($File . "_Locked", "");       //create new file with same name with "_Locked" in the end (second parameter must be specified)

$File_Content = file_get_contents($File);

$File_Content .= $_POST["String"];

sleep($Sleep_Time);       //sleep for x seconds

file_put_contents($File, $File_Content);

unlink($File . "_Locked");                  //delete the above "_Locked" file

echo "String Added";
return;
}
?>

Each request takes <?php echo $Sleep_Time ?> seconds to finish!
<br><br>
<input type="button"  value="Add A" onclick='Add_String("A")'>
<input type="button"  value="Add B" onclick='Add_String("B")'>
<br><br>
<div id="Ajax_Response">Ajax Response:<br><br></div>


<script>

function Add_String(Option){      //____________________________

var http = new XMLHttpRequest();
http.open('POST', "");      //blank url (send to same page)

http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');     //necessary to send "String" POST key below to php

    http.onreadystatechange = function(){
        if (this.readyState === 4) {     //"4", request finished and response is ready!
        document.getElementById("Ajax_Response").innerHTML += this.responseText + "<br>";
        }
    };

http.send('String=' + Option);
}

</script>
无论如何,我们欢迎更好的解决方案

<?php

$Sleep_Time = "3";

if (isset($_POST["String"])){

$File = "Edit_File_Content.txt";

$File_Write_Request = Check_File_Write_Queue($File);      //add a new write request to a file and wait here until older requests are finhished

$File_Content = file_get_contents($File);

$File_Content .= $_POST["String"];

sleep($Sleep_Time);       //sleep for x seconds

file_put_contents($File, $File_Content);

Check_File_Write_Queue($File_Write_Request, "Delete");
//if the above line is not used, the "register_shutdown_function()" used in the function will ensure that the "$File_Write_Request" will be removed when script exits!

echo "String Added";
return;
}

function Check_File_Write_Queue($File, $Option = "") {        //____________________________

$Files_Write_Queue = realpath("Edit_File_Content_Check_File_Write_Queue_List.txt");

    if ($Option == "Delete" || $Option == "delete"){
    $File_Content = file_get_contents($Files_Write_Queue);
    $File_Content = str_replace($File, "", $File_Content);
    file_put_contents($Files_Write_Queue, $File_Content);
    return;
    }

$File = realpath($File);

$Request_Id = microtime() . " " . bin2hex(random_bytes(10));

$Write_Request = $File . "<<<<" . $Request_Id . ">>>>\r\n";

file_put_contents($Files_Write_Queue, $Write_Request, FILE_APPEND);

    //to prevent errors\issues, "realpath()" should be used in "register_shutdown_function()"
    register_shutdown_function(function() use ($Files_Write_Queue,$Write_Request){
    $File_Content = file_get_contents($Files_Write_Queue);
    $File_Content = str_replace($Write_Request, "", $File_Content);
    file_put_contents($Files_Write_Queue, $File_Content);
    });

    while(1) {      //1=Infinite loop
    $File_Content = file_get_contents($Files_Write_Queue);
    preg_match('/' . preg_quote($File, '/') . '<<<<(.*?)>>>>/', $File_Content, $match);
    
    if (@$match[1] == $Request_Id){return $Write_Request;}
    }
} 

?>

Each request takes <?php echo $Sleep_Time ?> seconds to finish!
<br><br>
<input type="button"  value="Add A" onclick='Add_String("A")'>
<input type="button"  value="Add B" onclick='Add_String("B")'>
<br><br>
<div id="Ajax_Response">Ajax Response:<br><br></div>


<script>

function Add_String(Option){      //____________________________

var http = new XMLHttpRequest();
http.open('POST', "");      //blank url (send to same page)

http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');     //necessary to send "String" POST key below to php

    http.onreadystatechange = function(){
        if (this.readyState === 4) {     //"4", request finished and response is ready!
        document.getElementById("Ajax_Response").innerHTML += this.responseText + "<br>";
        }
    };

http.send('String=' + Option);
}

</script>

每个请求需要几秒钟才能完成!




Ajax响应:

函数Add_字符串(选项){//____________________________ var http=new XMLHttpRequest(); http.open('POST',“”);//空白url(发送到同一页面) http.setRequestHeader('Content-type','application/x-www-form-urlencoded');//将下面的“String”POST键发送到php所必需的 http.onreadystatechange=函数(){ 如果(this.readyState==4){/“4”,请求完成,响应准备就绪! document.getElementById(“Ajax_响应”).innerHTML+=this.responseText+“
”; } }; send('String='+选项); }
在前面的回答中,我使用了“Check\u File\u Write\u Queue()”函数,但有人提醒我,PHP内置函数“flock()”也可以用来做同样的事情,这是正确的,但我注意到了一件有趣的事情,“Check\u File\u Write\u Queue()”比“flock()”更精确,例如,如果一次单击“添加A”5次,然后单击“添加B”一次单击5次,来自“Check_File_Write_Queue()”的“Edit_File_Content.txt”文件中的输出将始终是“aaaaa bbbbbbb”,而来自“flock()”的输出将有所不同,“aaababbbba”、“BBAABBAABA”、“ababbaba”、“bbbbbbbbb aaaa”、“aaaaaaaabbbbb”等,这意味着“Check_File_Write_Queue()”始终以正确的顺序处理请求,“flock()”有时做,有时不做

无论如何,下面的代码是另一个使用PHP内置函数“flock()”而不是“Check_File_Write_Queue()”函数的解决方案:

<?php

$Sleep_Time = "3";

if (isset($_POST["String"])){

    $File = "Edit_File_Content.txt";
    $File_Handle = fopen("$File", "r");
    if(flock($File_Handle, LOCK_EX)){       //on success, this script execution waits here until older scripts in queue unlock the file

    $File_Content = file_get_contents($File);

    $File_Content .= $_POST["String"];

    sleep($Sleep_Time);       //sleep for x seconds

    file_put_contents($File, $File_Content);

    flock($File_Handle, LOCK_UN);       //unlock the file so new scripts in queue can continue execution

    echo "flock() success [String Added]";
    
    }else{echo "flock() Failed";}
    fclose($File_Handle);

return;
}

?>

Each request takes <?php echo $Sleep_Time ?> seconds to finish!
<br><br>
<input type="button"  value="Add A" onclick='Add_String("A")'>
<input type="button"  value="Add B" onclick='Add_String("B")'>
<br><br>
<div id="Ajax_Response">Ajax Response:<br><br></div>

<script>

function Add_String(Option){      //____________________________

var http = new XMLHttpRequest();
http.open('POST', "");      //blank url (send to same page)

http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');     //necessary to send "String" POST key below to php

http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');     //necessary to send "String" POST key below to php

    http.onreadystatechange = function(){
        if (this.readyState === 4) {     //"4", request finished and response is ready!
        document.getElementById("Ajax_Response").innerHTML += this.responseText + "<br>";
        }
    };

http.send('String=' + Option);
}

</script>

每个请求需要几秒钟才能完成!




Ajax响应:

函数Add_字符串(选项){//____________________________ var http=new XMLHttpRequest(); http.open('POST',“”);//空白url(发送到同一页面) http.setRequestHeader('Content-type','application/x-www-form-urlencoded');//将下面的“String”POST键发送到php所必需的 http.setRequestHeader('Content-type','application/x-www-form-urlencoded');//将下面的“String”POST键发送到php所必需的 http.onreadystatechange=函数(){ 如果(this.readyState==4){/“4”,请求完成,响应准备就绪! document.getElementById(“Ajax_响应”).innerHTML+=this.responseText+“
”; } }; send('String='+选项); }
与服务器的连接是无状态的,在第一个PHP脚本休眠期间,其他线程正在处理传入的请求,服务器不会等待第一个脚本执行。@Teemu,是的,我知道,这就是为什么我在代码示例中使用“while()”,但不幸的是,它没有按预期工作!