function useTick($concurrent, $numberOfJobs) { $logger = new Logger('swarm_logger'); $logger->pushProcessor(new MemoryUsageProcessor()); $swarm = new SwarmProcess($logger); $swarm->setMaxRunStackSize($concurrent); $counter = 0; // Now go run it: do { // If we have work to give the stack, then let's give it: if (++$counter <= $numberOfJobs) { $swarm->pushNativeCommandOnQueue(getCommand()); } } while ($swarm->tick()); }
*/ use Afrihost\SwarmProcess\SwarmProcess; use Monolog\Logger; use Symfony\Component\Process\Process; chdir(__DIR__); require '../vendor/autoload.php'; $logger = new Logger('swarm_logger'); $swarmProcess = new SwarmProcess($logger); // Build a list of things we'll be adding later on $add_these = array(); $add_these[] = new Process('sleep 9'); $add_these[] = new Process('sleep 8'); $add_these[] = new Process('sleep 7'); $add_these[] = new Process('sleep 6'); $add_these[] = new Process('sleep 5'); $add_these[] = new Process('sleep 5'); $add_these[] = new Process('sleep 4'); $add_these[] = new Process('sleep 3'); $add_these[] = new Process('sleep 2'); $add_these[] = new Process('sleep 1'); $swarmProcess->setMaxRunStackSize(4); do { try { if (count($add_these) > 0) { $swarmProcess->pushProcessOnQueue(array_shift($add_these)); } } catch (Exception $e) { // do something intelligent with the exception - but do not let the loop end, you will lose work } } while ($swarmProcess->tick());