Esempio n. 1
0
<?php

require_once __DIR__ . '/../autoload.php';
echo "Process 500 Tasks with an avg 1.5secs (min 1sec, max 2secs)\n";
echo "\t- Without the workerpool this would take about 12.5 minutes (avg)\n";
$timeused = microtime(true);
$wp = new \QXS\WorkerPool\WorkerPool();
$wp->setWorkerPoolSize(100)->create(new \QXS\WorkerPool\ClosureWorker(function ($input, $semaphore, $storage) {
    usleep(rand(1000000, 2000000));
    // this is the working load!
    return NULL;
}));
for ($i = 0; $i < 500; $i++) {
    $wp->run($i);
}
$wp->waitForAllWorkers();
// wait for all workers
$timeused = microtime(true) - $timeused;
echo "\t- With the workerpool it took: " . number_format($timeused, 2) . " seconds\n";
echo "\t- In this example the workerpool is " . number_format(750 / $timeused, 2) . " times faster!\n";
echo "\t- BTW: This is a simulation of a real world example, where we were waiting for remote results. This initiated the development of the workerpool.\n";
Esempio n. 2
0
<?php

require_once __DIR__ . '/../autoload.php';
$wp = new \QXS\WorkerPool\WorkerPool();
$wp->setWorkerPoolSize(4)->create(new \QXS\WorkerPool\ClosureWorker(function ($input, $semaphore, $storage) {
    sleep(rand(1, 3));
    // this is the working load!
    return $input;
}));
$i = 20;
while ($i < 40) {
    // is there a free worker?
    if ($wp->getFreeWorkers() > 0) {
        $wp->run($i);
        $i++;
    } else {
        // poll some data
        while ($wp->hasResults() && $wp->getFreeWorkers() == 0) {
            $val = $wp->getNextResult();
            echo "Received: " . $val['data'] . "    from pid " . $val['pid'] . "\n";
        }
        // still no free workers?
        if ($wp->getFreeWorkers() == 0) {
            usleep(1000);
            // and sleep a bit
        }
    }
}
while ($wp->hasResults() || $wp->getBusyWorkers() > 0) {
    // poll some data
    foreach ($wp as $val) {