1. Go to this page and download the library: Download afrihost/swarm-process 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/ */
afrihost / swarm-process example snippets
use Afrihost\SwarmProcess\SwarmProcess;
$swarm = new SwarmProcess(); // you may provide a Psr/Logger here if you want to
$swarm->setMaxRunStackSize(20); // default is 10 (cannot make it <= 0)
// Just some mock things for it to do:
for ($i = 0; $i < 10000; $i++) {
$swarm->pushNativeCommandOnQueue('echo "test"');
// Some examples of how to use it - note the new Process way!
// $swarm->pushNativeCommandOnQueue('ls');
// $swarm->pushNativeCommandOnQueue('sleep 10');
// $swarm->pushProcessOnQueue(new Process('ls'));
}
// Now we tell it to go run all 10k things, but to adhere to the 20 concurrent rule set above:
$swarm->run();
do {
// do nothing
} while ($swarm->tick());
do {
if ($db->hasMoreStuffToAddDummyFunction()) {
$swarm->pushProcessOnQueue(new Process($db->getSomeCommandToAddToTheQueue()));
}
} while ($swarm->tick());
$swarm->run(
function() {
// do a check to see if we should have more commands added to the queue
return new Process('sleep 5');
},
function () {
// check if the loop should still continue, if so return true
return true;
}
);
$logger = new Logger('swarm_logger');
$closure = function(Process $process) use ($logger) {
$logger->warning('Do something, like checking the exit codes: '.$process->getExitCode().' ['.$process->getExitCodeText().']');
};
$swarmProcess = new SwarmProcess($logger, (new Configuration())->setCompletedCallback($closure));
$logger = new Logger('swarm_logger');
$configuration = (new Configuration())
->setEnforceProcessTimeouts(true);
$swarmProcess = new SwarmProcess($logger, $configuration);
// two Processes, both set to timeout at 5 seconds
$swarmProcess->pushProcessOnQueue(new Process('sleep 9', null, null, null, 5));
$swarmProcess->pushProcessOnQueue(new Process('sleep 2', null, null, null, 5));
$swarmProcess->setMaxRunStackSize(4);
$swarmProcess->run();
$logger = new Logger('swarm_logger');
$configuration = (new Configuration())
->setTickLoopDelayMicroseconds(100000); // uses usleep, so microseconds.
$swarmProcess = new SwarmProcess($logger, $configuration);