PHP code example of soarce / parallel-process-dispatcher
1. Go to this page and download the library: Download soarce/parallel-process-dispatcher 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/ */
soarce / parallel-process-dispatcher example snippets
$process = new Process('pngcrush --brute background.png');
$process->start();
// optional: do something else in your application
while (! $process->isFinished() ) {
usleep(1000); //wait 1ms until next poll
}
echo $process->getOutput();
$process1 = new Process('pngcrush --brute background.png');
$process2 = new Process('pngcrush --brute welcome.png');
$process3 = new Process('pngcrush --brute logo.png');
$dispatcher = new Dispatcher(2); // will make sure only two of those will actually run at the same time
$dispatcher->addProcess($process1);
$dispatcher->addProcess($process2);
$dispatcher->addProcess($process3);
$dispatcher->dispatch(); // this will run until all processes are finished.
$processes = $dispatcher->getFinishedProcesses();
foreach ($processes as $process) {
echo $process->getOutput(), "\n\n";
}
$dispatcher = new Dispatcher(2);
$process1 = new Process('pngcrush --brute background.png');
$dispatcher->addProcess($process1, true); // true starts the process if there are still free slots
// [... more code ...]
$process2 = new Process('pngcrush --brute welcome.png');
$dispatcher->addProcess($process2, true);
// [... more code ...]
// during code execution, the dispatcher cannot remove finished processes from the stack, so you have to call the tick()-function
// if you want the queue to advance - but it's optional since at latest the __destruct() function will call dispatch();
$dispatcher->tick();
// [... more code ...]
$dispatcher->dispatch(); // this will make the dispatcher wait until all the processes are finished, if they are still running
$processes = $dispatcher->getFinishedProcesses();
// loop over results
$dispatcher = new Dispatcher(2);
$dispatcher->addProcess(new ProcessLineOutput("...", 'job1'));
$dispatcher->addProcess(new ProcessLineOutput("...", 'job2'));
$dispatcher->addProcess(new ProcessLineOutput("...", 'job3'));
$oa = new OutputAggregator($dispatcher);
foreach ($oa->getOutput() as $job => $line) {
echo $job, ': ', $line;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.