开发者

PHP script unexpectedly not continuing

开发者 https://www.devze.com 2023-04-08 20:39 出处:网络
I have a PHP script (let\'s call it execute.php) 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 t

I have a PHP script (let's call it execute.php) 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 results.php) because results.php needs the POST-information from the previous script.

execute.php:

<?php
print"
<html>
<body>
   Some HTML code here
</body>
</html>
";

// 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 "
<form action=\"results.php\" id=\"go_to_results\" method=\"POST\">
<input type='hidden' name=\"session_id\" value=\"XYZ\">
</form>
<script type=\"text/javascript\">
AutoSubmitForm( 'go_to_results' );
</script>
";

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.

However, when I put a echo("."); in the first while-loop before the sleep(), it works and continues until the JavaScript autosubmit.

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 while loop.

I have also tried using set_time_limit(0) and ignore_user_abort(true) 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.

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 echo(".").

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.

EDIT 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:

    process.php:
    <?php
    $params = "someparam=1";
    ?>

    <html>
    <body>
    <script type="text/javascript">
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 && http_request.status==200){
            // Maybe used to display the progress of the execution
            //document.getElementById("output").innerHTML=http_request.responseText;

            // Cal开发者_开发问答l of programs is finished -> 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);
};
</script>

<?php
// Do some HTML-markup
...
// Start the programs!
print "
<script type=\"text/javascript\">
    run_analyses('".$params."');
</script>
<form action=\"results.html" id=\"go_to_results\" method=\"POST\">
<input type='hidden' name=\"session_id\" value=\"XYZ\">
</form>
?>
</html>
</body>

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.

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.)?

EDIT 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.


It occurs to me that a better approach would be to:

  1. Output the complete HTML page
  2. Show a loading message to the user
  3. Send an AJAX request to start the external program
  4. Wait for callback (waiting for external program to finish)
  5. Repeat steps 3 and 4 until all program have been executed
  6. Update the page to tell the user what is going on
  7. Submit the form

This way, you get the HTML to the user as quickly as possible, then you execute the programs sequentially in an orderly and controlled fashion without worrying about hitting the max_execution_time threshold. This also enables you to keep your user informed - after each AJAX callback, you can tell the user that "program ABC has completed, starting DEF..." and so on.

EDIT

Per request, I'll add an outline of how this could be implemented. A caveat, too: If you are going to be adding more javascript-derived functionality to your page, you'll want to consider using a library like jQuery or mootools (my personal favorite). This is a decision you should make right away - if you aren't going to be doing a lot of javascript except this, then a library will only bloat your project, but if you are going to be adding a lot of javascript, you don't want to have to come back later and re-write your code because you add a library 3/4 of the way through the project.

I've used mootools to create this demonstration, but it isn't necessary or even advisable to add mootools if this is the only thing you're going to use it for. It is simply easier for me to write an example really quick without having to stop and think :)

First, the main page. We'll call this page view.php. This should contain your initial HTML as well as the javascript that will fire off the AJAX requests. Basically, this entire jsFiddle would be view.php: http://jsfiddle.net/WPnEy/1/

Now, execute.php looks like this:

$program_name = isset($_POST['program_name']) ? $_POST['program_name'] : false;
switch ($program_name) {
    case 'program1':
        $program_path = '/path/to/executable/';
        $friendly_name = 'Some program 1';
        break;
    case 'program2':
        $program_path = '/path/to/executable/';
        $friendly_name = 'Some program 2';
        break;
    case 'program3':
        $program_path = '/path/to/executable/';
        $friendly_name = 'Some program 3';
        break;
    case 'program4':
        $program_path = '/path/to/executable/';
        $friendly_name = 'Some program 4';
        break;
    default:
        die(json_encode(array(
            'program_name'=>'Invalid',
            'status'=>'FAILED',
            'error'->true,
            'error_msg'=>'Invalid program'
        )));
        break;
}

$pid = run_in_background($program_path)
while (is_running(pid)) {
  sleep(1);
}

// check here for errors, get any error messages you might have
$error = false;
$error_msg = '';
// use this for failures that are not necessarily errors...
$status = 'OK';

die(json_encode(array(
    'program_name'=>$friendly_name,
    'status'=>$status,
    'error'->$error,
    'error_msg'=>$error_msg
)));

execute.php would then be called once for each program. The $friendly_program variable gives you a way to send back something for the user to see. The switch statement there makes sure that the script isn't being asked to execute anything you aren't expecting. The program is executed, and when it is done you send along a little package of information with the status, the friendly name, any errors, etc. This comes into the javascript on view.php, which then decides if there are more programs to run. If so, it will call execute.php again... if not, it will submit the form.


This seems rather convoluted... And very risky. Any network glitches, the user's browser closing for whatever reason, and even a firewall timing out, and this script is aborted.

Why not run the whole thing in the background?

<?php

session_start();
$_SESSION['background_run_is_done'] = false;
session_write_close(); // release session file lock

set_time_limit(0); 
ignore_user_abort(true); // allow job to keep running even if client disconnects.

.... your external stuff here ...

if ($successfully_completed) {
   session_start(); // re-open session file to update value
   $_SESSION['background_run_is_done'] = TRUE;
}

... use curl to submit job completion post here ...

?>

This disconnects the state of the user's browser from the processing of the jobs. You then just have your client-side code ping the server occasionally to monitor the job's progress.


Launching and managing multiple and long-running processes from a webserver PHP process is fraught with complications and complexity. It's also very different on different platforms (you didn't say which you are using).

Handling the invocation of these processes synchronously from the execution of your PHP is not the way to address this. You really need to run the programs in a seperate session group - and use (e.g.) Ajax or Comet to poll the status of them.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号