PHP code example of console-helpers / process-iterator

1. Go to this page and download the library: Download console-helpers/process-iterator 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/ */

    

console-helpers / process-iterator example snippets




use ConsoleHelpers\ProcessIterator\ProcessIterator;
use Symfony\Component\Process\Process;

$processes = array(
	'a.txt' => new Process('wc -c a.txt'),
	'b.txt' => new Process('wc -c b.txt'),
	'c.txt' => new Process('wc -c c.txt'),
);


// All of the processes will be started at once, when "foreach" line is executed.
$process_iterator = new ProcessIterator($processes);

foreach ($process_iterator as $key => $process) {
	$stderr = $process->getErrorOutput();
	$stdout = $process->getOutput();
	do_some_processing($stdout);
}


// Will only run 2 processes in parallel at a time.
$process_iterator = new ProcessIterator($processes);
$process_iterator->limit(2);

foreach ($process_iterator as $key => $process) {
	$stderr = $process->getErrorOutput();
	$stdout = $process->getOutput();
	do_some_processing($stdout);
}


// Will run all processes in parallel. The $processes array can be inspected later 
// to see execution results.
$process_iterator = new ProcessIterator($processes);
$process_iterator->runAll();


// Allows to add more processes in real time as they are processed.
$process_iterator = new ProcessIterator($processes);

foreach ($process_iterator as $key => $process) {
	$stderr = $process->getErrorOutput();
	$stdout = $process->getOutput();
	do_some_processing($stdout);

	if ( $key === 'b.txt' ) {
		$process_iterator->addProcess(
			new Process('wc -c d.txt'),
			'd.txt'
		);
	}
}


// Show "processing ..." message at if no process was finished executing after 
// given time has passed. This can happen several times as well.
$process_iterator = new ProcessIterator($processes);
$process_iterator->setUpdateInterval(1);

foreach ($process_iterator as $key => $process) {
	if ($process === null) {
		echo "Still working...\n";
	}
	else {
		$stderr = $process->getErrorOutput();
		$stdout = $process->getOutput();
		do_some_processing($stdout);
	}
}


// Safe process exception detection. When exception happens during process 
// execution it's recorded and that process is immediately yielded. Then
// the $process_iterator->getProcessException() method can be used to 
// handle it gracefully (e.g. re-add back to queue).
$process_iterator = new ProcessIterator($processes);

foreach ($process_iterator as $key => $process) {
	$process_exception = $process_iterator->getException();

	if ( $process_exception instanceof ProcessTimedOutException ) {
		echo "The $key process timed out.\n";
	}
	elseif ( $process_exception instanceof ProcessFailedException ) {
		echo "The $key process has failed.\n";
	}
	else {
		$stderr = $process->getErrorOutput();
		$stdout = $process->getOutput();
		do_some_processing($stdout);
	}
}