Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It occurs to me that a better approach would be to:</p> <ol> <li>Output the complete HTML page</li> <li>Show a loading message to the user</li> <li>Send an AJAX request to start the external program</li> <li>Wait for callback (waiting for external program to finish)</li> <li>Repeat steps 3 and 4 until all program have been executed</li> <li>Update the page to tell the user what is going on</li> <li>Submit the form</li> </ol> <p>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 <code>max_execution_time</code> 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.</p> <p><strong>EDIT</strong></p> <p>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.</p> <p>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 :)</p> <p>First, the main page. We'll call this page <code>view.php</code>. 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: <a href="http://jsfiddle.net/WPnEy/1/" rel="nofollow">http://jsfiddle.net/WPnEy/1/</a></p> <p>Now, <code>execute.php</code> looks like this:</p> <pre><code>$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'=&gt;'Invalid', 'status'=&gt;'FAILED', 'error'-&gt;true, 'error_msg'=&gt;'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'=&gt;$friendly_name, 'status'=&gt;$status, 'error'-&gt;$error, 'error_msg'=&gt;$error_msg ))); </code></pre> <p><code>execute.php</code> would then be called once for each program. The <code>$friendly_program</code> 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 <code>view.php</code>, which then decides if there are more programs to run. If so, it will call <code>execute.php</code> again... if not, it will submit the form.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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