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';
|
2020-12-12 13:05:48 +01:00
|
|
|
require_once __DIR__ . '/classes/Counter.php';
|
2020-12-11 01:25:38 +01:00
|
|
|
const NUM_JOBS = 50;
|
|
|
|
|
2020-12-12 02:11:05 +01:00
|
|
|
$context = ContextBuilder::create()
|
2020-12-12 13:05:48 +01:00
|
|
|
->withCleanupInterval(Interval::seconds(0.5))
|
|
|
|
->build();
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
$counter = new Counter();
|
2020-12-12 02:11:05 +01:00
|
|
|
$context->on('worker_stopped', [$counter, 'increment']);
|
|
|
|
$context->on('no_workers_remaining', [$context, 'stop']);
|
2020-12-12 13:05:48 +01:00
|
|
|
$context->on('stopped', fn() => printf(" %d\n", $counter->value));
|
2020-12-11 01:25:38 +01:00
|
|
|
|
|
|
|
for ($i = 0; $i < NUM_JOBS; $i++) {
|
2020-12-12 13:05:48 +01:00
|
|
|
$context->submit(fn() => sleep(2));
|
|
|
|
print('.');
|
2020-12-11 01:25:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$context->run();
|