示例#1
0
<?php

namespace Gielfeldt\SimpleWorker\Example;

require 'vendor/autoload.php';
use Gielfeldt\SimpleWorker\Pool;
use Gielfeldt\SimpleWorker\Test\SimpleTestWorker;
$pool = new Pool(['concurrency' => 1]);
$time = microtime(true);
$workers = [];
for ($i = 0; $i < 10; $i++) {
    $workers[] = new SimpleTestWorker(uniqid(), 0.5);
}
print "Adding worker\n";
$pool->addWorkers($workers, function ($worker) use($time) {
    $elapsed = microtime(true) - $time;
    print "\n{$worker->key} is ready! Elapsed time: {$elapsed}\n";
});
print "Processing workers\n";
$pool->process(['progress_callback' => function ($pool) {
    print ".";
}, 'finish_callback' => function ($pool) {
    print "\n";
}]);
示例#2
0
 /**
  * Test callbacks.
  *
  * @covers \Gielfeldt\SimpleWorker\Pool::addWorker
  * @covers \Gielfeldt\SimpleWorker\Pool::process
  */
 public function testCallbacks()
 {
     $progress = 0;
     $counter = 0;
     $finished = false;
     $options = [];
     $options['progress_callback'] = function () use(&$progress) {
         $progress++;
     };
     $options['finish_callback'] = function () use(&$finished) {
         $finished = true;
     };
     $pool = new Pool($options);
     $callback = function () use(&$counter) {
         $counter++;
     };
     $pool->addWorker(new SimpleTestWorker('key1', 1), $callback);
     $pool->addWorker(new SimpleTestWorker('key2', 1), $callback);
     $pool->addWorker(new SimpleTestWorker('key3', 1), $callback);
     $pool->process();
     $this->assertTrue($finished, 'Finish callback was not executed.');
     $this->assertTrue(is_numeric($progress) && $progress > 0, 'Progress counter was not incremented correctly.');
     $this->assertEquals(3, $counter, 'Counter was not incremented correctly.');
 }