PHP code example of ravisorg / exec-parallel

1. Go to this page and download the library: Download ravisorg/exec-parallel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ravisorg / exec-parallel example snippets


use \ExecParallel\Controller;
use \ExecParallel\Job;

// Create the ExecParallel controller.
$jobControl = new Controller();

// Maximum number of simultaneous jobs you want running. You can queue up more than this and 
// Controller will start more as others finish.
$jobControl->maxJobs = 2;

// Set an event / callback when the first job starts, and another when all jobs are complete. 
// Controller triggers all pass in the Controller object as the first parameter for easy 
// reference. Some events may pass in additional parameters as well.
$jobControl->on('start',function($controller) { print "Starting jobs...\n"; });
$jobControl->on('complete',function($controller) { print "Completed all jobs...\n"; });

// Add a job
$job = new Job();
$job->command = 'sleep 5 && echo "5 seconds complete!"';

// Set some events for when the job starts, completes, or writes to stdout/stderr. Job triggers all
// $job,$output) { print "Echo job output: $output\n"; });
$job->on('stderr',function($job,$output) { print "Echo job output to ERR: $output\n"; });
$job->on('complete',function($job,$exitCode) { print "Completed the echo job, exited with ".var_export($exitCode,true).".\n"; });

// If you want to pass data to the command on stdin, uncomment and set appropriately...
// $job->stdin = 'Input to pass to command';

// Add the job
$jobControl->addJob($job);

// Start jobs and wait until all jobs are complete
$jobControl->waitUntilComplete();

print "Total run time: ".$jobControl->runTime." sec\n";

while ($jobControl->process()) {
	// do some things in here
}

print "Total run time: ".$jobControl->runTime." sec\n";