This article analyzes the solution to the batch task processing without timeout in PHP. Share it for your reference, as follows:
PHP batch task processing
PHP will time out when batch processing tasks. In fact, the solution is very simple. It is to split the task and process part of it at a time. The task progress can be placed on the server or on the client. If it is not very complicated, it is placed on the client and used js to process it.
Client js callback processing
When the client processes, you need to live in a place, that is, when using ajax, ajax is asynchronous, and when using a for loop to process, it is just a batch request. In this way, when the task is large, it will directly DDOS server, so you need to wait for the callback function to return and then make the next request.
Client Example
document:
<!DOCTYPE html> <html> <head> <title></title> <script type="text/javascript" src="/jquery/1.11.3/"></script> <script type="text/javascript"> $(function(){ $("#Jidsall").click(function(){ $(".Jids").prop("checked", ); }); $("#btn_request").click(function(){ // Task Object var task = {}; // Task list = $(".Jids:checked").toArray(); // Current task = 0; // Next request = function() { if ( >= ) { // Mission completed (); return; } var i = ; // Request failed var error = function(data){ // The logic of failure ("error", ); // Continue to call (); }; // Request succeeded var success = function(data){ // Logic of success ("success", ); // Continue to call (); }; $.ajax({ context: this, method: "post", url: "", data: {id:[i].value}, error: error, success: success, dataType: "json" }); ++; }; // Complete the request = function() { ("done"); }; // ask (); }); }); </script> </head> <body> <table> <tr><td><input type="checkbox" >all</td></tr> <tr><td><input type="checkbox" value="1" class="Jids">1</td></tr> <tr><td><input type="checkbox" value="2" class="Jids">2</td></tr> <tr><td><input type="checkbox" value="3" class="Jids">3</td></tr> <tr><td><input type="checkbox" value="4" class="Jids">4</td></tr> <tr><td><input type="checkbox" value="5" class="Jids">5</td></tr> <tr><td><input type="checkbox" value="6" class="Jids">6</td></tr> <tr><td><input type="checkbox" value="7" class="Jids">7</td></tr> <tr><td><input type="checkbox" value="8" class="Jids">8</td></tr> <tr><td><input type="checkbox" value="9" class="Jids">9</td></tr> <tr><td><input type="button" value="ask"></td></tr> </table> </body> </html>
Example of PHP processing batch tasks Server side example
document:
<?php sleep(3); if ($_POST["id"] == 5) { http_response_code(500); exit(); } echo json_encode($_POST);
For more information about PHP related content, please check out the topic of this site:Summary of usage of php strings》、《Complete collection of PHP array (Array) operation techniques》、《Summary of PHP operations and operator usage》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《Summary of php operation office documentation skills (including word, excel, access, ppt)》、《Summary of the usage of php date and time》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.