javascript - 如何在 PHP 中对请求进行排队?
问题描述
我尝试了下面的代码来排队请求,但它没有按预期工作!
<?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>
下图显示了同时单击“添加 A”和“添加 B”5 次时的“Ajax 响应”:
在所有请求完成后,“Edit_File_Content.txt”文件将只包含“AA”而不是“AAAAABBBBB”字符串!
关于如何在 php 中有效地对请求进行排队的任何建议?
解决方案
我写了下面的“Check_File_Write_Queue()”函数,到目前为止,它似乎工作正常!
当“添加 A”和“添加 B”同时单击 5 次时,当所有请求完成时,“Edit_File_Content.txt”文件将按预期包含“AAAAABBBBB”字符串!
无论如何,总是欢迎更好的解决方案!
<?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>
推荐阅读
- python - 无限循环中的Python脚本
- r - 如何合并2个数据集?
- pyspark - 如何将前导零添加到 pyspark 数据框列
- react-native - 将数据发送到 bottomTabNavigator 中的每个屏幕
- scala - 在 Scala 中读取带有多行字符串的 CSV 文件
- firebase - 如何在 Firestore 中测试零文档
- android - Parcelable 对象中的字符串资源字段的值并不总是可以通过 getString() 访问
- pdf - 使用 pandoc 创建带有 css 和 html 的 PDF
- java - Gradle 设置以处理具有相同代码库但依赖项不同的两种部署类型
- javascript - 如何创建返回有效负载的 Redux Saga