2020-12-11 01:25:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Toalett\Multiprocessing\ContextBuilder;
|
2020-12-12 02:11:05 +01:00
|
|
|
use Toalett\Multiprocessing\Task\Interval;
|
2020-12-11 01:25:38 +01:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
|
|
|
|
|
|
const NUM_JOBS = 50;
|
|
|
|
|
|
|
|
$counter = new class {
|
|
|
|
public int $value = 0;
|
|
|
|
|
|
|
|
public function increment(): void
|
|
|
|
{
|
|
|
|
$this->value++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-12 02:11:05 +01:00
|
|
|
$context = ContextBuilder::create()
|
|
|
|
->withCleanupInterval(Interval::seconds(0.5))
|
|
|
|
->build();
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 02:11:05 +01:00
|
|
|
$context->on('worker_stopped', [$counter, 'increment']);
|
|
|
|
$context->on('no_workers_remaining', [$context, 'stop']);
|
|
|
|
$context->on('stopped', fn() => printf("\nJobs completed: %d\n", $counter->value));
|
2020-12-11 01:25:38 +01:00
|
|
|
|
|
|
|
for ($i = 0; $i < NUM_JOBS; $i++) {
|
|
|
|
$context->submit(fn() => sleep(3));
|
2020-12-12 02:11:05 +01:00
|
|
|
print('.');
|
2020-12-11 01:25:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$context->run();
|