Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP script unexpectedly not continuing
    primarykey
    data
    text
    <p>I have a PHP script (let's call it <code>execute.php</code>) that draws the whole page (HTML tags and body tags etc.) at the beginning and, afer that, executes some commands (C++ programs) in the background. It then waits for these programs to terminate (some depend on the results of others, so they may be executed sequentially) and then has a JavaScript that auto-submits a form to another PHP script (which we will call <code>results.php</code>) because <code>results.php</code> needs the POST-information from the previous script.</p> <p><code>execute.php</code>:</p> <pre><code>&lt;?php print" &lt;html&gt; &lt;body&gt; Some HTML code here &lt;/body&gt; &lt;/html&gt; "; // Here come some C++-program calls $pid_program1 = run_in_background($program1) $pid_program2 = run_in_background($program2) while (is_running($pid_program1) or is_running($pid_program2) ) { //echo("."); sleep(1); } // Here come some later C++-program calls that execute quickly $pid_program3 = run_in_background($program3) $pid_program4 = run_in_background($program4) while (is_running($pid_program3) or is_running($pid_program4) ) { sleep(1); } ... // We are now finished print " &lt;form action=\"results.php\" id=\"go_to_results\" method=\"POST\"&gt; &lt;input type='hidden' name=\"session_id\" value=\"XYZ\"&gt; &lt;/form&gt; &lt;script type=\"text/javascript\"&gt; AutoSubmitForm( 'go_to_results' ); &lt;/script&gt; "; </code></pre> <p>This works nicely if the C++ programs 1 and 2 execute quickly. However, when they take their time (around 25 minutes in total), the PHP script seems to fail to continue. Interestingly the C++ programs 3 and 4 are nevertheless executed and produce the expected outputs etc.</p> <p>However, when I put a <code>echo(".");</code> in the first while-loop before the <code>sleep()</code>, it works and continues until the JavaScript autosubmit.</p> <p>So it seems to me that the remaining PHP code (including the autosubmit) is, for whatever reason, not send when there is no output in the first <code>while</code> loop.</p> <p>I have also tried using <code>set_time_limit(0)</code> and <code>ignore_user_abort(true)</code> and different other things like writing to an outputbuffer (don't want to clutter the already finally displayed webpage) instead of the echo, but none of these work.</p> <p>When I run the same scripts on a machine with multiple cores, so that program1 and 2 can be executed in parallel, it also works, without the <code>echo(".")</code>.</p> <p>So I am currently very confused and can't find any error messages in the apache log or PHP log and thus would really appreciate your thoughts on this one.</p> <p><strong>EDIT</strong> Thanks again for your suggestions so far. I have now adopted a solution involving (really simple) AJAX and it's definitely nicer this way. However, if the C++-programs executions take "longer" it is not autosubmitting to the results-page, which is actually created this time (failed to do so before). Basically what I have done is:</p> <pre><code> process.php: &lt;?php $params = "someparam=1"; ?&gt; &lt;html&gt; &lt;body&gt; &lt;script type="text/javascript"&gt; function run_analyses(params){ // Use AJAX to execute the programs independenantly in the background // Allows for the user to close the process-page and come back at a later point to the results-link, w/o need to wait. if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); } else { //Fallback for IE5 and IE6, as these don't support the above writing/code http_request = new ActiveXObject("Microsoft.XMLHTTP"); } //Is http_request still false if (!http_request) { alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen'); } http_request.onreadystatechange=function(){ if (http_request.readyState==4 &amp;&amp; http_request.status==200){ // Maybe used to display the progress of the execution //document.getElementById("output").innerHTML=http_request.responseText; // Call of programs is finished -&gt; Go to the results-page document.getElementById( "go_to_results" ).submit(); } }; http_request.open("POST","execute.php",true); //Send the proper header information along with the request http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http_request.setRequestHeader("Content-length", params.length); http_request.setRequestHeader("Connection", "close"); http_request.send(params); }; &lt;/script&gt; &lt;?php // Do some HTML-markup ... // Start the programs! print " &lt;script type=\"text/javascript\"&gt; run_analyses('".$params."'); &lt;/script&gt; &lt;form action=\"results.html" id=\"go_to_results\" method=\"POST\"&gt; &lt;input type='hidden' name=\"session_id\" value=\"XYZ\"&gt; &lt;/form&gt; ?&gt; &lt;/html&gt; &lt;/body&gt; </code></pre> <p>and execute.php contains the C++-program calls, waiting-routines and finally, via "include("results.php")" the creation of the results-page. Again, for "not so long" program executions, the autosubmission works as expected, but not if it takes "longer". By "longer" I mean around 25 minutes.</p> <p>I have absolutely no idea what could cause this as again, there are no error-messages to be found. Am I missing a crucial configuration option there (apache, php, etc.)?</p> <p><strong>EDIT</strong> As it turned out, letting the requested PHP-script "echo" something repeatedly prevents the timeout. So it is basically the same as for the PHP-solution without AJAX, but this time, since the responseText of the AJAX-request is not necessarily needed, the progress-page is not cluttered and it may be used as a workaround. Specifically, I would not necessarily recommend it a as a general solution or good-practice.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload