toalett-multiprocessing/bin/simple_example.php

33 lines
681 B
PHP
Raw Normal View History

<?php
use Toalett\Multiprocessing\ContextBuilder;
2020-12-12 02:11:05 +01:00
use Toalett\Multiprocessing\Task\Interval;
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-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));
for ($i = 0; $i < NUM_JOBS; $i++) {
$context->submit(fn() => sleep(3));
2020-12-12 02:11:05 +01:00
print('.');
}
$context->run();