Пример #1
0
function useCallable($concurrent, $numberOfJobs)
{
    $logger = new Logger('swarm_logger');
    $logger->pushProcessor(new MemoryUsageProcessor());
    $listOfProcessesToRun = array();
    for ($i = 0; $i < $numberOfJobs; $i++) {
        $listOfProcessesToRun[] = getCommand();
    }
    $swarm = new SwarmProcess($logger);
    $swarm->setMaxRunStackSize($concurrent);
    // Now go run it:
    $swarm->run(function () {
        usleep(50000);
        if (rand(0, 3) == 0) {
            return new Process(getCommand());
        } else {
            echo 'skipped...' . PHP_EOL;
        }
    }, function () {
        static $runcount = 0;
        $runcount++;
        echo '>>>>>>>>>>>>>>>>>>>>>>>>> ' . $runcount . PHP_EOL;
        return $runcount < 5;
    });
}
Пример #2
0
<?php

/**
 * This examples shows the simplest way of using the SwarmProcess class.
 * The logger is used purely to allow you to see what's happening.
 * If you don't provide the logger, the Psr\Log\NullLogger will be used internally.
 */
use Afrihost\SwarmProcess\SwarmProcess;
use Monolog\Logger;
chdir(__DIR__);
require '../vendor/autoload.php';
$logger = new Logger('swarm_logger');
$swarmProcess = new SwarmProcess($logger);
// Add a few things to do:
$swarmProcess->pushNativeCommandOnQueue('sleep 9');
$swarmProcess->pushNativeCommandOnQueue('sleep 8');
$swarmProcess->pushNativeCommandOnQueue('sleep 7');
$swarmProcess->pushNativeCommandOnQueue('sleep 6');
$swarmProcess->pushNativeCommandOnQueue('sleep 5');
$swarmProcess->pushNativeCommandOnQueue('sleep 5');
$swarmProcess->pushNativeCommandOnQueue('sleep 4');
$swarmProcess->pushNativeCommandOnQueue('sleep 3');
$swarmProcess->pushNativeCommandOnQueue('sleep 2');
$swarmProcess->pushNativeCommandOnQueue('sleep 1');
$swarmProcess->setMaxRunStackSize(4);
$swarmProcess->run();
use Symfony\Component\Process\Process;
chdir(__DIR__);
require '../vendor/autoload.php';
$logger = new Logger('swarm_logger');
$swarmProcess = new SwarmProcess($logger);
// Add a few things to do:
$swarmProcess->pushProcessOnQueue(new Process('sleep 9'));
$swarmProcess->pushProcessOnQueue(new Process('sleep 8'));
$swarmProcess->pushProcessOnQueue(new Process('sleep 7'));
$swarmProcess->pushProcessOnQueue(new Process('sleep 6'));
$swarmProcess->pushProcessOnQueue(new Process('sleep 5'));
$swarmProcess->pushProcessOnQueue(new Process('sleep 5'));
$swarmProcess->pushProcessOnQueue(new Process('sleep 4'));
$swarmProcess->pushProcessOnQueue(new Process('sleep 3'));
$swarmProcess->pushProcessOnQueue(new Process('sleep 2'));
$swarmProcess->pushProcessOnQueue(new Process('sleep 1'));
$swarmProcess->setMaxRunStackSize(4);
/**
 * @return null|Process
 */
$closureAddMoreStuff = function () {
    if (rand(0, 1000) == 0) {
        // just some randomization to illustrate the point of real world
        return new Process('sleep 5');
    }
    return null;
};
$swarmProcess->run($closureAddMoreStuff, function () {
    return true;
    // this could be any range of logic, a DB call, anything you want
});